Skip to contentSkip to conversation

Move closer to upstream

PATCH Applied Glauber Costa 3 participants 13 messages Jul 14, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover Move closer to upstream

Same thing that was already sent, but rebased for today's tree. Avi suggested me to just keep the include for libkvm-all.h and he would fold it manually.

Note that I'm _not_ doing this here, because marcelo, who is less bright than avi, seem to prefer me to do this way.

If applied on top of today's tree with no modifications, it should have no conflicts.

#1 lore raw

PATCH v3 1/8 replace USE_KVM with CONFIG_KVM

Make things less confuse, and we have KVM_UPSTREAM to differentiate between the two versions anyway. kvm-all.c and kvm.c gets compiled now, but protected with KVM_UPSTREAM too, so no function in there gets visible in the final binary

---
 Makefile.target   |   20 ++++++--------------
 configure         |   14 +++++++-------
 hw/acpi.c         |    4 ++--
 hw/msix.c         |    2 +-
 hw/virtio-net.c   |    4 ++--
 kvm-all.c         |    2 ++
 libkvm-all.h      |    6 +++---
 qemu-kvm.h        |    2 +-
 target-i386/kvm.c |    2 ++
 vl.c              |    8 ++++----
 10 files changed, 30 insertions(+), 34 deletions(-)
Makefile.target +6 -14
index e3189a1..e0edd27 100644
@@ -160,9 +160,7 @@ ifeq ($(ARCH),sparc64)
 CPPFLAGS+=-I$(SRC_PATH)/tcg/sparc
 endif
 
-ifeq ($(USE_KVM), 1)
-libobj-y += qemu-kvm.o
-endif
+libobj-$(CONFIG_KVM) += qemu-kvm.o
 ifdef CONFIG_SOFTFLOAT
 libobj-y += fpu/softfloat.o
 else
@@ -173,18 +171,14 @@ libobj-y += op_helper.o helper.o
 
 ifeq ($(TARGET_ARCH), i386)
 libobj-y += helper.o
-ifeq ($(USE_KVM), 1)
-libobj-y += qemu-kvm-x86.o kvm-tpr-opt.o
-libobj-y += qemu-kvm-helper.o
-endif
+libobj-$(CONFIG_KVM) += qemu-kvm-x86.o kvm-tpr-opt.o
+libobj-$(CONFIG_KVM) += qemu-kvm-helper.o
 endif
 
 ifeq ($(TARGET_ARCH), x86_64)
 libobj-y += helper.o
-ifeq ($(USE_KVM), 1)
-libobj-y += qemu-kvm-x86.o kvm-tpr-opt.o
-libobj-y += qemu-kvm-helper.o
-endif
+libobj-$(CONFIG_KVM) += qemu-kvm-x86.o kvm-tpr-opt.o
+libobj-$(CONFIG_KVM) += qemu-kvm-helper.o
 endif
 
 libobj-y += op_helper.o
@@ -203,9 +197,7 @@ endif
 
 ifeq ($(TARGET_BASE_ARCH), ia64)
 libobj-y += op_helper.o firmware.o
-ifeq ($(USE_KVM), 1)
-libobj-y += qemu-kvm-ia64.o
-endif
+libobj-$(CONFIG_KVM) += qemu-kvm-ia64.o
 endif
 
 ifeq ($(TARGET_BASE_ARCH), cris)
configure +7 -7
index f8b80f2..9b744c5 100755
@@ -2121,8 +2121,8 @@ disable_cpu_emulation() {
 configure_kvm() {
   if test "$kvm" = "yes" -a "$target_softmmu" = "yes" -a \
           \( "$cpu" = "i386" -o "$cpu" = "x86_64" -o "$cpu" = "ia64" -o "$cpu" = "powerpc" \); then
-    echo "#define USE_KVM 1" >> $config_h
-    echo "USE_KVM=1" >> $config_mak
+    echo "#define CONFIG_KVM 1" >> $config_h
+    echo "CONFIG_KVM=y" >> $config_mak
     echo "KVM_CFLAGS=$kvm_cflags" >> $config_mak
     if test $kvm_cap_pit = "yes" ; then
 	echo "USE_KVM_PIT=1" >> $config_mak
@@ -2159,9 +2159,9 @@ case "$target_arch2" in
       echo "#define CONFIG_KQEMU 1" >> $config_h
     fi
     if test "$target_kvm" = "yes" ; then
-      echo "USE_KVM=yes" >> $config_mak
+      echo "CONFIG_KVM=y" >> $config_mak
       echo "KVM_CFLAGS=$kvm_cflags" >> $config_mak
-      echo "#define USE_KVM 1" >> $config_h
+      echo "#define CONFIG_KVM 1" >> $config_h
     fi
     if test "$xen" = "yes" -a "$target_softmmu" = "yes";
     then
@@ -2183,9 +2183,9 @@ case "$target_arch2" in
     fi
     if [ use_upstream_kvm = yes ]; then
     if test "$target_kvm" = "yes" ; then
-      echo "USE_KVM=yes" >> $config_mak
+      echo "CONFIG_KVM=y" >> $config_mak
       echo "KVM_CFLAGS=$kvm_cflags" >> $config_mak
-      echo "#define USE_KVM 1" >> $config_h
+      echo "#define CONFIG_KVM 1" >> $config_h
     fi
     fi
     if test "$xen" = "yes" -a "$target_softmmu" = "yes"
@@ -2281,7 +2281,7 @@ case "$target_arch2" in
     if test "$target_kvm" = "yes" ; then
       echo "CONFIG_KVM=y" >> $config_mak
       echo "KVM_CFLAGS=$kvm_cflags" >> $config_mak
-      echo "#define USE_KVM 1" >> $config_h
+      echo "#define CONFIG_KVM 1" >> $config_h
     fi
     fi
     gdb_xml_files="power-core.xml power-fpu.xml power-altivec.xml power-spe.xml"
hw/acpi.c +2 -2
index 074e09f..7de9cb7 100644
@@ -775,7 +775,7 @@ static void disable_processor(struct gpe_regs *g, int cpu)
 }
 
 #if defined(TARGET_I386) || defined(TARGET_X86_64)
-#ifdef USE_KVM
+#ifdef CONFIG_KVM
 static CPUState *qemu_kvm_cpu_env(int index)
 {
     CPUState *penv;
@@ -798,7 +798,7 @@ void qemu_system_cpu_hot_add(int cpu, int state)
     CPUState *env;
 
     if (state
-#ifdef USE_KVM
+#ifdef CONFIG_KVM
         && (!qemu_kvm_cpu_env(cpu))
 #endif
     ) {
hw/msix.c +1 -1
index 5f77dc9..b5dfa0b 100644
@@ -63,7 +63,7 @@
 /* Flag for interrupt controller to declare MSI-X support */
 int msix_supported;
 
-#ifdef USE_KVM
+#ifdef CONFIG_KVM
 /* KVM specific MSIX helpers */
 static void kvm_msix_free(PCIDevice *dev)
 {
hw/virtio-net.c +2 -2
index 6b82232..bda2397 100644
@@ -15,7 +15,7 @@
 #include "net.h"
 #include "qemu-timer.h"
 #include "virtio-net.h"
-#ifdef USE_KVM
+#ifdef CONFIG_KVM
 #include "qemu-kvm.h"
 #endif
 
@@ -344,7 +344,7 @@ static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
 
     qemu_flush_queued_packets(n->vc);
 
-#ifdef USE_KVM
+#ifdef CONFIG_KVM
     /* We now have RX buffers, signal to the IO thread to break out of the
        select to re-poll the tap file descriptor */
     if (kvm_enabled())
kvm-all.c +2 -0
index 8567ac9..4c2fdf5 100644
@@ -26,6 +26,7 @@
 #include "gdbstub.h"
 #include "kvm.h"
 
+#ifdef KVM_UPSTREAM
 /* KVM uses PAGE_SIZE in it's definition of COALESCED_MMIO_MAX */
 #define PAGE_SIZE TARGET_PAGE_SIZE
 
@@ -1027,3 +1028,4 @@ void kvm_remove_all_breakpoints(CPUState *current_env)
 {
 }
 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
+#endif
libkvm-all.h +3 -3
index f36d3c3..e28f950 100644
@@ -5,7 +5,7 @@
 #ifndef LIBKVM_H
 #define LIBKVM_H
 
-#ifdef USE_KVM
+#ifdef CONFIG_KVM
 
 #if defined(__s390__)
 #include <asm/ptrace.h>
@@ -925,10 +925,10 @@ int kvm_assign_set_msix_entry(kvm_context_t kvm,
 
 uint32_t kvm_get_supported_cpuid(kvm_context_t kvm, uint32_t function, int reg);
 
-#else /* !USE_KVM */
+#else /* !CONFIG_KVM */
 
 struct kvm_pit_state { };
 
-#endif /* !USE_KVM */
+#endif /* !CONFIG_KVM */
 
 #endif
qemu-kvm.h +1 -1
index 2153afc..333109f 100644
@@ -138,7 +138,7 @@ void kvm_arch_do_ioperm(void *_data);
 #define ALIGN(x, y)  (((x)+(y)-1) & ~((y)-1))
 #define BITMAP_SIZE(m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) / 8)
 
-#ifdef USE_KVM
+#ifdef CONFIG_KVM
 #include "sys-queue.h"
 
 extern int kvm_allowed;
target-i386/kvm.c +2 -0
index 1da1543..b7eb096 100644
@@ -24,6 +24,7 @@
 #include "cpu.h"
 #include "gdbstub.h"
 
+#ifdef KVM_UPSTREAM
 //#define DEBUG_KVM
 
 #ifdef DEBUG_KVM
@@ -962,3 +963,4 @@ void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg)
     }
 }
 #endif /* KVM_CAP_SET_GUEST_DEBUG */
+#endif
vl.c +4 -4
index 5949acb..357043a 100644
@@ -5434,14 +5434,14 @@ int main(int argc, char **argv, char **envp)
                 break;
 #endif
 #ifdef CONFIG_KVM
+#ifdef KVM_UPSTREAM
             case QEMU_OPTION_enable_kvm:
                 kvm_allowed = 1;
 #ifdef CONFIG_KQEMU
                 kqemu_allowed = 0;
 #endif
-                break;
 #endif
-#ifdef USE_KVM
+                break;
 	    case QEMU_OPTION_no_kvm:
 		kvm_allowed = 0;
 		break;
@@ -5750,7 +5750,7 @@ int main(int argc, char **argv, char **envp)
         signal(SIGTTIN, SIG_IGN);
     }
 
-#ifdef USE_KVM
+#ifdef CONFIG_KVM
     if (kvm_enabled()) {
 	if (kvm_qemu_init() < 0) {
 	    fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
@@ -6023,7 +6023,7 @@ int main(int argc, char **argv, char **envp)
 
     if (kvm_enabled()) {
         kvm_init_ap();
-#ifdef USE_KVM
+#ifdef CONFIG_KVM
         if (kvm_irqchip) {
             if (!qemu_kvm_has_gsi_routing()) {
                 irq0override = 0;
Signature · 1 line
1.6.2.2
#2 lore raw

PATCH v3 2/8 Do not compile qemu-kvm.c and qemu-kvm-x86.c

Instead, include them from upstream files

---
 Makefile.target   |    5 ++---
 kvm-all.c         |    2 ++
 target-i386/kvm.c |    2 ++
 3 files changed, 6 insertions(+), 3 deletions(-)
Makefile.target +2 -3
index e0edd27..df1f32b 100644
@@ -160,7 +160,6 @@ ifeq ($(ARCH),sparc64)
 CPPFLAGS+=-I$(SRC_PATH)/tcg/sparc
 endif
 
-libobj-$(CONFIG_KVM) += qemu-kvm.o
 ifdef CONFIG_SOFTFLOAT
 libobj-y += fpu/softfloat.o
 else
@@ -171,13 +170,13 @@ libobj-y += op_helper.o helper.o
 
 ifeq ($(TARGET_ARCH), i386)
 libobj-y += helper.o
-libobj-$(CONFIG_KVM) += qemu-kvm-x86.o kvm-tpr-opt.o
+libobj-$(CONFIG_KVM) += kvm-tpr-opt.o
 libobj-$(CONFIG_KVM) += qemu-kvm-helper.o
 endif
 
 ifeq ($(TARGET_ARCH), x86_64)
 libobj-y += helper.o
-libobj-$(CONFIG_KVM) += qemu-kvm-x86.o kvm-tpr-opt.o
+libobj-$(CONFIG_KVM) += kvm-tpr-opt.o
 libobj-$(CONFIG_KVM) += qemu-kvm-helper.o
 endif
 
kvm-all.c +2 -0
index 4c2fdf5..e42b1f6 100644
@@ -1029,3 +1029,5 @@ void kvm_remove_all_breakpoints(CPUState *current_env)
 }
 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
 #endif
+
+#include "qemu-kvm.c"
target-i386/kvm.c +2 -0
index b7eb096..cfa5b80 100644
@@ -964,3 +964,5 @@ void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg)
 }
 #endif /* KVM_CAP_SET_GUEST_DEBUG */
 #endif
+
+#include "qemu-kvm-x86.c"
Signature · 1 line
1.6.2.2
#3 lore raw

PATCH v3 3/8 replace malloc with qemu_malloc

This patch replaces both malloc and malloc+memset sequences with qemu_malloc and qemu_mallocz. Target is upstream integration

---
 qemu-kvm-x86.c |   31 ++++++++-----------------------
 qemu-kvm.c     |   26 +++++---------------------
 2 files changed, 13 insertions(+), 44 deletions(-)
qemu-kvm-x86.c +8 -23
index daf60b6..e953391 100644
@@ -390,12 +390,9 @@ struct kvm_msr_list *kvm_get_msr_list(kvm_context_t kvm)
 		return NULL;
 	/* Old kernel modules had a bug and could write beyond the provided
 	   memory. Allocate at least a safe amount of 1K. */
-	msrs = malloc(MAX(1024, sizeof(*msrs) +
-				sizer.nmsrs * sizeof(*msrs->indices)));
-	if (!msrs) {
-		errno = ENOMEM;
-		return NULL;
-	}
+	msrs = qemu_malloc(MAX(1024, sizeof(*msrs) +
+				       sizer.nmsrs * sizeof(*msrs->indices)));
+
 	msrs->nmsrs = sizer.nmsrs;
 	r = ioctl(kvm->fd, KVM_GET_MSR_INDEX_LIST, msrs);
 	if (r == -1) {
@@ -409,13 +406,9 @@ struct kvm_msr_list *kvm_get_msr_list(kvm_context_t kvm)
 
 int kvm_get_msrs(kvm_vcpu_context_t vcpu, struct kvm_msr_entry *msrs, int n)
 {
-    struct kvm_msrs *kmsrs = malloc(sizeof *kmsrs + n * sizeof *msrs);
+    struct kvm_msrs *kmsrs = qemu_malloc(sizeof *kmsrs + n * sizeof *msrs);
     int r, e;
 
-    if (!kmsrs) {
-	errno = ENOMEM;
-	return -1;
-    }
     kmsrs->nmsrs = n;
     memcpy(kmsrs->entries, msrs, n * sizeof *msrs);
     r = ioctl(vcpu->fd, KVM_GET_MSRS, kmsrs);
@@ -428,13 +421,9 @@ int kvm_get_msrs(kvm_vcpu_context_t vcpu, struct kvm_msr_entry *msrs, int n)
 
 int kvm_set_msrs(kvm_vcpu_context_t vcpu, struct kvm_msr_entry *msrs, int n)
 {
-    struct kvm_msrs *kmsrs = malloc(sizeof *kmsrs + n * sizeof *msrs);
+    struct kvm_msrs *kmsrs = qemu_malloc(sizeof *kmsrs + n * sizeof *msrs);
     int r, e;
 
-    if (!kmsrs) {
-	errno = ENOMEM;
-	return -1;
-    }
     kmsrs->nmsrs = n;
     memcpy(kmsrs->entries, msrs, n * sizeof *msrs);
     r = ioctl(vcpu->fd, KVM_SET_MSRS, kmsrs);
@@ -524,9 +513,7 @@ int kvm_setup_cpuid(kvm_vcpu_context_t vcpu, int nent,
 	struct kvm_cpuid *cpuid;
 	int r;
 
-	cpuid = malloc(sizeof(*cpuid) + nent * sizeof(*entries));
-	if (!cpuid)
-		return -ENOMEM;
+	cpuid = qemu_malloc(sizeof(*cpuid) + nent * sizeof(*entries));
 
 	cpuid->nent = nent;
 	memcpy(cpuid->entries, entries, nent * sizeof(*entries));
@@ -542,9 +529,7 @@ int kvm_setup_cpuid2(kvm_vcpu_context_t vcpu, int nent,
 	struct kvm_cpuid2 *cpuid;
 	int r;
 
-	cpuid = malloc(sizeof(*cpuid) + nent * sizeof(*entries));
-	if (!cpuid)
-		return -ENOMEM;
+	cpuid = qemu_malloc(sizeof(*cpuid) + nent * sizeof(*entries));
 
 	cpuid->nent = nent;
 	memcpy(cpuid->entries, entries, nent * sizeof(*entries));
@@ -632,7 +617,7 @@ static struct kvm_cpuid2 *try_get_cpuid(kvm_context_t kvm, int max)
 	int r, size;
 
 	size = sizeof(*cpuid) + max * sizeof(*cpuid->entries);
-	cpuid = (struct kvm_cpuid2 *)malloc(size);
+	cpuid = qemu_malloc(size);
 	cpuid->nent = max;
 	r = ioctl(kvm->fd, KVM_GET_SUPPORTED_CPUID, cpuid);
 	if (r == -1)
qemu-kvm.c +5 -21
index ed7e466..cf97ba2 100644
@@ -447,10 +447,7 @@ kvm_context_t kvm_init(void *opaque)
 	}
 	kvm_abi = r;
 	kvm_page_size = getpagesize();
-	kvm = malloc(sizeof(*kvm));
-	if (kvm == NULL)
-		goto out_close;
-	memset(kvm, 0, sizeof(*kvm));
+	kvm = qemu_mallocz(sizeof(*kvm));
 	kvm->fd = fd;
 	kvm->vm_fd = -1;
 	kvm->opaque = opaque;
@@ -464,10 +461,7 @@ kvm_context_t kvm_init(void *opaque)
 
 		/* Round up so we can search ints using ffs */
 		gsi_bits = ALIGN(gsi_count, 32);
-		kvm->used_gsi_bitmap = malloc(gsi_bits / 8);
-		if (!kvm->used_gsi_bitmap)
-			goto out_close;
-		memset(kvm->used_gsi_bitmap, 0, gsi_bits / 8);
+		kvm->used_gsi_bitmap = qemu_mallocz(gsi_bits / 8);
 		kvm->max_gsi = gsi_bits;
 
 		/* Mark any over-allocated bits as already in use */
@@ -507,12 +501,7 @@ kvm_vcpu_context_t kvm_create_vcpu(kvm_context_t kvm, int id)
 {
 	long mmap_size;
 	int r;
-	kvm_vcpu_context_t vcpu_ctx = malloc(sizeof(struct kvm_vcpu_context));
-
-	if (!vcpu_ctx) {
-		errno = ENOMEM;
-		return NULL;
-	}
+	kvm_vcpu_context_t vcpu_ctx = qemu_malloc(sizeof(struct kvm_vcpu_context));
 
 	vcpu_ctx->kvm = kvm;
 	vcpu_ctx->id = id;
@@ -559,10 +548,7 @@ int kvm_create_vm(kvm_context_t kvm)
 	int fd = kvm->fd;
 
 #ifdef KVM_CAP_IRQ_ROUTING
-	kvm->irq_routes = malloc(sizeof(*kvm->irq_routes));
-	if (!kvm->irq_routes)
-		return -ENOMEM;
-	memset(kvm->irq_routes, 0, sizeof(*kvm->irq_routes));
+	kvm->irq_routes = qemu_mallocz(sizeof(*kvm->irq_routes));
 	kvm->nr_allocated_irq_routes = 0;
 #endif
 
@@ -1167,9 +1153,7 @@ int kvm_set_signal_mask(kvm_vcpu_context_t vcpu, const sigset_t *sigset)
 			r = -errno;
 		return r;
 	}
-	sigmask = malloc(sizeof(*sigmask) + sizeof(*sigset));
-	if (!sigmask)
-		return -ENOMEM;
+	sigmask = qemu_malloc(sizeof(*sigmask) + sizeof(*sigset));
 
 	sigmask->len = 8;
 	memcpy(sigmask->sigset, sigset, sizeof(*sigset));
Signature · 1 line
1.6.2.2
#4 lore raw

PATCH v3 4/8 fold libkvm-all into standard qemu header

Sharing of structures containing each other between libkvm-all.h and qemu-kmv.h gets a bit messy in this series. So fold them together. libkvm-all.h has no place in the final schema of things anyway.

---
 kvm.h                |    1 -
 libkvm-all.h         |  934 --------------------------------------------------
 qemu-kvm-ia64.c      |    1 -
 qemu-kvm-x86.c       |    1 -
 qemu-kvm.c           |    1 -
 qemu-kvm.h           |  927 +++++++++++++++++++++++++++++++++++++++++++++++++-
 target-i386/libkvm.h |    2 -
 7 files changed, 926 insertions(+), 941 deletions(-)
 delete mode 100644 libkvm-all.h
kvm.h +0 -1
index d9723d7..e9a43e2 100644
@@ -16,7 +16,6 @@
 
 #include "config.h"
 #include "sys-queue.h"
-#include "libkvm-all.h"
 
 #ifdef KVM_UPSTREAM
 
libkvm-all.h deleted +0 -934

934 lines not on this page · raw message

qemu-kvm-ia64.c +0 -1
index 582113f..b8a2587 100644
@@ -5,7 +5,6 @@
 
 #include "hw/hw.h"
 #include "qemu-kvm.h"
-#include "libkvm-all.h"
 #include <pthread.h>
 #include <sys/utsname.h>
 #include <sys/io.h>
qemu-kvm-x86.c +0 -1
index e953391..b531ca4 100644
@@ -15,7 +15,6 @@
 #include <sys/io.h>
 
 #include "qemu-kvm.h"
-#include "libkvm-all.h"
 #include "libkvm.h"
 #include <pthread.h>
 #include <sys/utsname.h>
qemu-kvm.c +0 -1
index cf97ba2..43e7b4c 100644
@@ -19,7 +19,6 @@
 #include "gdbstub.h"
 
 #include "qemu-kvm.h"
-#include "libkvm-all.h"
 #include "libkvm.h"
 
 #include <pthread.h>
qemu-kvm.h +926 -1

933 lines not on this page · raw message

target-i386/libkvm.h +0 -2
index f3fc6dc..d85b6a1 100644
@@ -18,8 +18,6 @@
 #ifndef KVM_X86_H
 #define KVM_X86_H
 
-#include "libkvm-all.h"
-
 #define PAGE_SIZE 4096ul
 #define PAGE_MASK (~(PAGE_SIZE - 1))
 
Signature · 1 line
1.6.2.2
#5 lore raw

PATCH v3 5/8 duplicate KVMState

In this patch, we duplicate most of KVMState in our files. This should be removed later, when they are 100 % equal. Meanwhile, we fold our kvm_context_t structure inside it.

To make transition smooth, we still keep a global variable kvm_context pointing to its position inside the global KVMState. This way we don't need to hurry about changing all callers.

kvm_init() and kvm_finalize are changed, though, since they have now to deal with the creation/destruction of a global KVMState

---
 qemu-kvm.c |   61 +++++++++++++++++++++++++++--------------------------------
 qemu-kvm.h |   36 +++++++++++++++++++++++-----------
 vl.c       |    2 +-
 3 files changed, 53 insertions(+), 46 deletions(-)
qemu-kvm.c +28 -33
index 43e7b4c..45f5abe 100644
@@ -42,6 +42,9 @@ int kvm_irqchip = 1;
 int kvm_pit = 1;
 int kvm_pit_reinject = 1;
 int kvm_nested = 0;
+
+
+static KVMState *kvm_state;
 kvm_context_t kvm_context;
 
 pthread_mutex_t qemu_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -416,16 +419,16 @@ int kvm_dirty_pages_log_reset(kvm_context_t kvm)
 }
 
 
-kvm_context_t kvm_init(void *opaque)
+int kvm_init(int smp_cpus)
 {
 	int fd;
-	kvm_context_t kvm;
 	int r, gsi_count;
 
+
 	fd = open("/dev/kvm", O_RDWR);
 	if (fd == -1) {
 		perror("open /dev/kvm");
-		return NULL;
+		return -1;
 	}
 	r = ioctl(fd, KVM_GET_API_VERSION, 0);
 	if (r == -1) {
@@ -446,35 +449,39 @@ kvm_context_t kvm_init(void *opaque)
 	}
 	kvm_abi = r;
 	kvm_page_size = getpagesize();
-	kvm = qemu_mallocz(sizeof(*kvm));
-	kvm->fd = fd;
-	kvm->vm_fd = -1;
-	kvm->opaque = opaque;
-	kvm->dirty_pages_log_all = 0;
-	kvm->no_irqchip_creation = 0;
-	kvm->no_pit_creation = 0;
+	kvm_state = qemu_mallocz(sizeof(*kvm_state));
+    kvm_context = &kvm_state->kvm_context;
 
-	gsi_count = kvm_get_gsi_count(kvm);
+	kvm_context->fd = fd;
+	kvm_context->vm_fd = -1;
+	kvm_context->opaque = cpu_single_env;
+	kvm_context->dirty_pages_log_all = 0;
+	kvm_context->no_irqchip_creation = 0;
+	kvm_context->no_pit_creation = 0;
+
+	gsi_count = kvm_get_gsi_count(kvm_context);
 	if (gsi_count > 0) {
 		int gsi_bits, i;
 
 		/* Round up so we can search ints using ffs */
 		gsi_bits = ALIGN(gsi_count, 32);
-		kvm->used_gsi_bitmap = qemu_mallocz(gsi_bits / 8);
-		kvm->max_gsi = gsi_bits;
+		kvm_context->used_gsi_bitmap = qemu_mallocz(gsi_bits / 8);
+		kvm_context->max_gsi = gsi_bits;
 
 		/* Mark any over-allocated bits as already in use */
 		for (i = gsi_count; i < gsi_bits; i++)
-			set_gsi(kvm, i);
+			set_gsi(kvm_context, i);
 	}
 
-	return kvm;
+    pthread_mutex_lock(&qemu_mutex);
+	return 0;
+
  out_close:
 	close(fd);
-	return NULL;
+	return -1;
 }
 
-void kvm_finalize(kvm_context_t kvm)
+static void kvm_finalize(KVMState *s)
 {
 	/* FIXME
 	if (kvm->vcpu_fd[0] != -1)
@@ -482,8 +489,8 @@ void kvm_finalize(kvm_context_t kvm)
 	if (kvm->vm_fd != -1)
 		close(kvm->vm_fd);
 	*/
-	close(kvm->fd);
-	free(kvm);
+	close(s->kvm_context.fd);
+	free(s);
 }
 
 void kvm_disable_irqchip_creation(kvm_context_t kvm)
@@ -2217,18 +2224,6 @@ int kvm_main_loop(void)
     return 0;
 }
 
-int kvm_qemu_init()
-{
-    /* Try to initialize kvm */
-    kvm_context = kvm_init(cpu_single_env);
-    if (!kvm_context) {
-      	return -1;
-    }
-    pthread_mutex_lock(&qemu_mutex);
-
-    return 0;
-}
-
 #ifdef TARGET_I386
 static int destroy_region_works = 0;
 #endif
@@ -2252,12 +2247,12 @@ int kvm_qemu_create_context(void)
         kvm_disable_pit_creation(kvm_context);
     }
     if (kvm_create(kvm_context, 0, NULL) < 0) {
-       kvm_finalize(kvm_context);
+       kvm_finalize(kvm_state);
        return -1;
     }
     r = kvm_arch_qemu_create_context();
     if(r <0)
-       kvm_finalize(kvm_context);
+       kvm_finalize(kvm_state);
     if (kvm_pit && !kvm_pit_reinject) {
         if (kvm_reinject_control(kvm_context, 0)) {
             fprintf(stderr, "failure to disable in-kernel PIT reinjection\n");
qemu-kvm.h +24 -12
index 20993f6..5d2d54c 100644
@@ -128,18 +128,7 @@ int kvm_set_msrs(kvm_vcpu_context_t, struct kvm_msr_entry *msrs, int n);
  * \param opaque Not used
  * \return NULL on failure
  */
-kvm_context_t kvm_init(void *opaque);
-
-/*!
- * \brief Cleanup the KVM context
- *
- * Should always be called when closing down KVM.\n
- * Exception: If kvm_init() fails, this function should not be called, as the
- * context would be invalid
- *
- * \param kvm Pointer to the kvm_context that is to be freed
- */
-void kvm_finalize(kvm_context_t kvm);
+int kvm_init(int smp_cpus);
 
 /*!
  * \brief Disable the in-kernel IRQCHIP creation
@@ -1166,4 +1155,27 @@ static inline int kvm_set_migration_log(int enable)
     return kvm_physical_memory_set_dirty_tracking(enable);
 }
 
+typedef struct KVMSlot
+{
+    target_phys_addr_t start_addr;
+    ram_addr_t memory_size;
+    ram_addr_t phys_offset;
+    int slot;
+    int flags;
+} KVMSlot;
+
+typedef struct kvm_dirty_log KVMDirtyLog;
+
+typedef struct KVMState
+{
+    KVMSlot slots[32];
+    int fd;
+    int vmfd;
+    int coalesced_mmio;
+    int broken_set_mem_region;
+    int migration_log;
+    struct kvm_context kvm_context;
+} KVMState;
+
+
 #endif
vl.c +1 -1
index 357043a..b3df596 100644
@@ -5752,7 +5752,7 @@ int main(int argc, char **argv, char **envp)
 
 #ifdef CONFIG_KVM
     if (kvm_enabled()) {
-	if (kvm_qemu_init() < 0) {
+	if (kvm_init(smp_cpus) < 0) {
 	    fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
 #ifdef NO_CPU_EMULATION
 	    fprintf(stderr, "Compiled with --disable-cpu-emulation, exiting.\n");
Signature · 1 line
1.6.2.2
#6 lore raw

PATCH v3 6/8 provide env->kvm_fd

qemu upstream puts kvm information on env. Do that too, since it will allow us to use CPUState in cpu-specific functions, instead of kvm-specific types.

---
 qemu-kvm.c |    9 +++++++--
 qemu-kvm.h |    2 +-
 2 files changed, 8 insertions(+), 3 deletions(-)
qemu-kvm.c +7 -2
index 45f5abe..6897e3c 100644
@@ -503,11 +503,12 @@ void kvm_disable_pit_creation(kvm_context_t kvm)
 	kvm->no_pit_creation = 1;
 }
 
-kvm_vcpu_context_t kvm_create_vcpu(kvm_context_t kvm, int id)
+kvm_vcpu_context_t kvm_create_vcpu(CPUState *env, int id)
 {
 	long mmap_size;
 	int r;
 	kvm_vcpu_context_t vcpu_ctx = qemu_malloc(sizeof(struct kvm_vcpu_context));
+    kvm_context_t kvm = kvm_context;
 
 	vcpu_ctx->kvm = kvm;
 	vcpu_ctx->id = id;
@@ -518,6 +519,10 @@ kvm_vcpu_context_t kvm_create_vcpu(kvm_context_t kvm, int id)
 		goto err;
 	}
 	vcpu_ctx->fd = r;
+
+    env->kvm_fd = r;
+    env->kvm_state = kvm_state;
+
 	mmap_size = ioctl(kvm->fd, KVM_GET_VCPU_MMAP_SIZE, 0);
 	if (mmap_size == -1) {
 		fprintf(stderr, "get vcpu mmap size: %m\n");
@@ -2013,7 +2018,7 @@ static void *ap_main_loop(void *_env)
     env->thread_id = kvm_get_thread_id();
     sigfillset(&signals);
     sigprocmask(SIG_BLOCK, &signals, NULL);
-    env->kvm_cpu_state.vcpu_ctx = kvm_create_vcpu(kvm_context, env->cpu_index);
+    env->kvm_cpu_state.vcpu_ctx = kvm_create_vcpu(env, env->cpu_index);
 
 #ifdef USE_KVM_DEVICE_ASSIGNMENT
     /* do ioperm for io ports of assigned devices */
qemu-kvm.h +1 -1
index 5d2d54c..fbbbb43 100644
@@ -181,7 +181,7 @@ void kvm_create_irqchip(kvm_context_t kvm);
  * \param slot vcpu number (> 0)
  * \return 0 on success, -errno on failure
  */
-kvm_vcpu_context_t kvm_create_vcpu(kvm_context_t kvm, int id);
+kvm_vcpu_context_t kvm_create_vcpu(CPUState *env, int id);
 
 /*!
  * \brief Start the VCPU
Signature · 1 line
1.6.2.2
#7 lore raw

PATCH v3 7/8 use kvm_upstream sw_breakpoints structure

---
 qemu-kvm-x86.c |    4 ++--
 qemu-kvm.c     |   27 ++++++++++++++++++---------
 qemu-kvm.h     |    9 ++++++---
 3 files changed, 26 insertions(+), 14 deletions(-)
qemu-kvm-x86.c +2 -2
index b531ca4..3bbb9d2 100644
@@ -1519,7 +1519,7 @@ int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info)
 			break;
 		    }
 	}
-    } else if (kvm_find_sw_breakpoint(arch_info->pc))
+    } else if (kvm_find_sw_breakpoint(cpu_single_env, arch_info->pc))
 	handle = 1;
 
     if (!handle)
@@ -1542,7 +1542,7 @@ void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg)
     };
     int n;
 
-    if (!TAILQ_EMPTY(&kvm_sw_breakpoints))
+    if (kvm_sw_breakpoints_active(env))
 	dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
 
     if (nb_hw_breakpoint > 0) {
qemu-kvm.c +18 -9
index 6897e3c..b0661b6 100644
@@ -459,6 +459,10 @@ int kvm_init(int smp_cpus)
 	kvm_context->no_irqchip_creation = 0;
 	kvm_context->no_pit_creation = 0;
 
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+    TAILQ_INIT(&kvm_state->kvm_sw_breakpoints);
+#endif
+
 	gsi_count = kvm_get_gsi_count(kvm_context);
 	if (gsi_count > 0) {
 		int gsi_bits, i;
@@ -2439,14 +2443,13 @@ int kvm_qemu_init_env(CPUState *cenv)
 }
 
 #ifdef KVM_CAP_SET_GUEST_DEBUG
-struct kvm_sw_breakpoint_head kvm_sw_breakpoints =
-    TAILQ_HEAD_INITIALIZER(kvm_sw_breakpoints);
 
-struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(target_ulong pc)
+struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env,
+                                                 target_ulong pc)
 {
     struct kvm_sw_breakpoint *bp;
 
-    TAILQ_FOREACH(bp, &kvm_sw_breakpoints, entry) {
+    TAILQ_FOREACH(bp, &env->kvm_state->kvm_sw_breakpoints, entry) {
 	if (bp->pc == pc)
 	    return bp;
     }
@@ -2481,6 +2484,11 @@ int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
     return data.err;
 }
 
+int kvm_sw_breakpoints_active(CPUState *env)
+{
+    return !TAILQ_EMPTY(&env->kvm_state->kvm_sw_breakpoints);
+}
+
 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
                           target_ulong len, int type)
 {
@@ -2489,7 +2497,7 @@ int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
     int err;
 
     if (type == GDB_BREAKPOINT_SW) {
-	bp = kvm_find_sw_breakpoint(addr);
+	bp = kvm_find_sw_breakpoint(current_env, addr);
 	if (bp) {
 	    bp->use_count++;
 	    return 0;
@@ -2507,7 +2515,8 @@ int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
 	    return err;
 	}
 
-	TAILQ_INSERT_HEAD(&kvm_sw_breakpoints, bp, entry);
+    TAILQ_INSERT_HEAD(&current_env->kvm_state->kvm_sw_breakpoints,
+                      bp, entry);
     } else {
 	err = kvm_arch_insert_hw_breakpoint(addr, len, type);
 	if (err)
@@ -2530,7 +2539,7 @@ int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
     int err;
 
     if (type == GDB_BREAKPOINT_SW) {
-	bp = kvm_find_sw_breakpoint(addr);
+	bp = kvm_find_sw_breakpoint(current_env, addr);
 	if (!bp)
 	    return -ENOENT;
 
@@ -2543,7 +2552,7 @@ int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
 	if (err)
 	    return err;
 
-	TAILQ_REMOVE(&kvm_sw_breakpoints, bp, entry);
+	TAILQ_REMOVE(&current_env->kvm_state->kvm_sw_breakpoints, bp, entry);
 	qemu_free(bp);
     } else {
 	err = kvm_arch_remove_hw_breakpoint(addr, len, type);
@@ -2564,7 +2573,7 @@ void kvm_remove_all_breakpoints(CPUState *current_env)
     struct kvm_sw_breakpoint *bp, *next;
     CPUState *env;
 
-    TAILQ_FOREACH_SAFE(bp, &kvm_sw_breakpoints, entry, next) {
+    TAILQ_FOREACH_SAFE(bp, &current_env->kvm_state->kvm_sw_breakpoints, entry, next) {
         if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
             /* Try harder to find a CPU that currently sees the breakpoint. */
             for (env = first_cpu; env != NULL; env = env->next_cpu) {
qemu-kvm.h +6 -3
index fbbbb43..d5291a3 100644
@@ -1004,12 +1004,12 @@ struct kvm_sw_breakpoint {
     int use_count;
     TAILQ_ENTRY(kvm_sw_breakpoint) entry;
 };
-TAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
 
-extern struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
+TAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
 
 int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info);
-struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(target_ulong pc);
+int kvm_sw_breakpoints_active(CPUState *env);
+struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env, target_ulong pc);
 int kvm_arch_insert_sw_breakpoint(CPUState *current_env,
                                   struct kvm_sw_breakpoint *bp);
 int kvm_arch_remove_sw_breakpoint(CPUState *current_env,
@@ -1174,6 +1174,9 @@ typedef struct KVMState
     int coalesced_mmio;
     int broken_set_mem_region;
     int migration_log;
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+    struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
+#endif
     struct kvm_context kvm_context;
 } KVMState;
 
Signature · 1 line
1.6.2.2
#8 lore raw

PATCH v3 8/8 reuse upstream breakpoint code

Drop KVM_UPSTREAM around functions we intend to reuse. This allow us to share code in kvm-all.c, that is equal in qemu-kvm.c

---
 kvm-all.c  |    5 ++-
 kvm.h      |    1 +
 qemu-kvm.c |  140 +-----------------------------------------------------------
 3 files changed, 6 insertions(+), 140 deletions(-)
kvm-all.c +4 -1
index e42b1f6..67908a7 100644
@@ -873,6 +873,8 @@ void kvm_setup_guest_memory(void *start, size_t size)
     }
 }
 
+#endif /* KVM_UPSTREAM */
+
 #ifdef KVM_CAP_SET_GUEST_DEBUG
 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env,
                                                  target_ulong pc)
@@ -891,6 +893,7 @@ int kvm_sw_breakpoints_active(CPUState *env)
     return !TAILQ_EMPTY(&env->kvm_state->kvm_sw_breakpoints);
 }
 
+#ifdef KVM_UPSTREAM
 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
 {
     struct kvm_guest_debug dbg;
@@ -904,6 +907,7 @@ int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
 
     return kvm_vcpu_ioctl(env, KVM_SET_GUEST_DEBUG, &dbg);
 }
+#endif
 
 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
                           target_ulong len, int type)
@@ -1028,6 +1032,5 @@ void kvm_remove_all_breakpoints(CPUState *current_env)
 {
 }
 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
-#endif
 
 #include "qemu-kvm.c"
kvm.h +1 -0
index e9a43e2..0191752 100644
@@ -16,6 +16,7 @@
 
 #include "config.h"
 #include "sys-queue.h"
+#include "qemu-kvm.h"
 
 #ifdef KVM_UPSTREAM
 
qemu-kvm.c +1 -139

152 lines not on this page · raw message

Signature · 1 line
1.6.2.2
#9 lore raw

Re: [PATCH v3 8/8] reuse upstream breakpoint code

Quoted 5 lines · Glauber Costa

Drop KVM_UPSTREAM around functions we intend to reuse. This allow us to share code in kvm-all.c, that is equal in qemu-kvm.c

Looks good (and demonstrates nicely how we can merge things in the ideal case).

Jan

#10 lore raw

Re: [PATCH v3 8/8] reuse upstream breakpoint code

Quoted 9 lines · Jan Kiszka
Quoted 5 lines · Glauber Costa

Drop KVM_UPSTREAM around functions we intend to reuse. This allow us to share code in kvm-all.c, that is equal in qemu-kvm.c

Looks good (and demonstrates nicely how we can merge things in the ideal case).

neat.
As per gleb suggestion, I also posted to qemu a patch that gives us on_vcpu(). We can share more code, once it is there.

Marcelo, can you please merge it?

#11 lore raw

Re: [PATCH v3 8/8] reuse upstream breakpoint code

Quoted 8 lines · Glauber Costa
Quoted 7 lines · Jan Kiszka
Quoted 5 lines · Glauber Costa

Drop KVM_UPSTREAM around functions we intend to reuse. This allow us to share code in kvm-all.c, that is equal in qemu-kvm.c

Quoted 7 lines
Quoted 2 lines

Looks good (and demonstrates nicely how we can merge things in the ideal case).

neat.
As per gleb suggestion, I also posted to qemu a patch that gives us on_vcpu(). We can share more code, once it is there.

Marcelo, can you please merge it?

Patches 6 and 7 contain some tab breakage, is that intentional?

#12 lore raw
In reply to Glauber Costa
Quoted 9 lines · Glauber Costa

Same thing that was already sent, but rebased for today's tree. Avi suggested me to just keep the include for libkvm-all.h and he would fold it manually.

Note that I'm _not_ doing this here, because marcelo, who is less bright than avi, seem to prefer me to do this way.

If applied on top of today's tree with no modifications, it should have no conflicts.

Applied, thanks.

The qemu-kvm.git branch i'm applying patches to is

git://git.kernel.org/pub/scm/linux/kernel/git/marcelo/qemu-kvm.git/ next

This is temporary, should be back to the normal URL in the next couple of days.