Skip to contentSkip to conversation

Further qemu-kvm cleanups

PATCH Applied Glauber Costa 2 participants 7 messages Jul 6, 2009 – Jul 7, 2009 KVM

lore
Share & tools
lore
#0 lore raw

v3 cover Further qemu-kvm cleanups

Now that I've definitely flushed my backlog, here's a resend of the last cleanup series I sent. It fixes the segfault avi noticed.

#1 lore raw

PATCH v3 1/5 qemu-kvm.c memory cleanup

Now that libkvm is merged into this file, there is a lot of things we don't need.

kvm_create_phys_mem() is only used by user/ files, and so can be removed from this implementation. kvm_cpu_register_physical_memory() morphs into kvm_set_phys_mem(), and uses qemu types.

---
 qemu-kvm.c |   70 +----------------------------------------------------------
 qemu-kvm.h |   16 +------------
 2 files changed, 4 insertions(+), 82 deletions(-)
qemu-kvm.c +2 -68
index c5cd038..44cea10 100644
@@ -534,52 +534,6 @@ int kvm_create(kvm_context_t kvm, unsigned long phys_mem_bytes, void **vm_mem)
 }
 
 
-void *kvm_create_phys_mem(kvm_context_t kvm, unsigned long phys_start,
-			  unsigned long len, int log, int writable)
-{
-	int r;
-	int prot = PROT_READ;
-	void *ptr;
-	struct kvm_userspace_memory_region memory = {
-		.memory_size = len,
-		.guest_phys_addr = phys_start,
-		.flags = log ? KVM_MEM_LOG_DIRTY_PAGES : 0,
-	};
-
-	if (writable)
-		prot |= PROT_WRITE;
-
-#if !defined(__s390__)
-	ptr = mmap(NULL, len, prot, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
-#else
-	ptr = mmap(LIBKVM_S390_ORIGIN, len, prot | PROT_EXEC,
-		MAP_FIXED | MAP_SHARED | MAP_ANONYMOUS, -1, 0);
-#endif
-	if (ptr == MAP_FAILED) {
-		fprintf(stderr, "%s: %s", __func__, strerror(errno));
-		return 0;
-	}
-
-	memset(ptr, 0, len);
-
-	memory.userspace_addr = (unsigned long)ptr;
-	memory.slot = get_free_slot(kvm);
-	DPRINTF("slot %d start %llx len %llx flags %x\n",
-		memory.slot,
-		memory.guest_phys_addr,
-		memory.memory_size,
-		memory.flags);
-	r = ioctl(kvm->vm_fd, KVM_SET_USER_MEMORY_REGION, &memory);
-	if (r == -1) {
-		fprintf(stderr, "%s: %s", __func__, strerror(errno));
-		return 0;
-	}
-	register_slot(memory.slot, memory.guest_phys_addr, memory.memory_size,
-		      memory.userspace_addr, memory.flags);
-
-        return ptr;
-}
-
 int kvm_register_phys_mem(kvm_context_t kvm,
 			  unsigned long phys_start, void *userspace_addr,
 			  unsigned long len, int log)
@@ -2394,9 +2348,8 @@ static void drop_mapping(target_phys_addr_t start_addr)
 }
 #endif
 
-void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
-                                      unsigned long size,
-                                      unsigned long phys_offset)
+void kvm_set_phys_mem(target_phys_addr_t start_addr, ram_addr_t size,
+                      ram_addr_t phys_offset)
 {
     int r = 0;
     unsigned long area_flags;
@@ -2473,13 +2426,6 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
     return;
 }
 
-void kvm_cpu_unregister_physical_memory(target_phys_addr_t start_addr,
-                                        target_phys_addr_t size,
-                                        unsigned long phys_offset)
-{
-    kvm_unregister_memory_area(kvm_context, start_addr, size);
-}
-
 int kvm_setup_guest_memory(void *area, unsigned long size)
 {
     int ret = 0;
@@ -2801,18 +2747,6 @@ int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf)
     return kvm_get_dirty_pages(kvm_context, phys_addr, buf);
 }
 
-void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
-			      unsigned long size, int log, int writable)
-{
-    return kvm_create_phys_mem(kvm_context, start_addr, size, log, writable);
-}
-
-void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
-			      unsigned long size)
-{
-    kvm_destroy_phys_mem(kvm_context, start_addr, size);
-}
-
 void kvm_mutex_unlock(void)
 {
     assert(!cpu_single_env);
qemu-kvm.h +2 -14
index 18a5b7f..de90259 100644
@@ -48,12 +48,8 @@ void qemu_kvm_cpuid_on_env(CPUState *env);
 void kvm_inject_interrupt(CPUState *env, int mask);
 void kvm_update_after_sipi(CPUState *env);
 void kvm_update_interrupt_request(CPUState *env);
-void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
-                                      unsigned long size,
-                                      unsigned long phys_offset);
-void kvm_cpu_unregister_physical_memory(target_phys_addr_t start_addr,
-                                        target_phys_addr_t size,
-                                        unsigned long phys_offset);
+void kvm_set_phys_mem(target_phys_addr_t start_addr, ram_addr_t size,
+                      ram_addr_t phys_offset);
 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
 			      unsigned long size, int log, int writable);
 
@@ -198,14 +194,6 @@ static inline void kvm_sleep_end(void)
 	kvm_mutex_lock();
 }
 
-static inline void kvm_set_phys_mem(target_phys_addr_t start_addr,
-                      ram_addr_t size,
-                      ram_addr_t phys_offset)
-{
-    kvm_cpu_register_physical_memory(start_addr, size, phys_offset);
-}
-
-
 int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr);
 
 int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len);
Signature · 1 line
1.6.2.2
#2 lore raw

PATCH v3 2/5 cleanup mmio coalescing functions

remove wrappers that existed only due to qemu/libkvm separation. Use qemu types for function definitions.

---
 qemu-kvm.c |   27 ++++-----------------------
 qemu-kvm.h |    5 -----
 2 files changed, 4 insertions(+), 28 deletions(-)
qemu-kvm.c +4 -23
index 44cea10..e4af96e 100644
@@ -1104,9 +1104,10 @@ int kvm_init_coalesced_mmio(kvm_context_t kvm)
 	return r;
 }
 
-int kvm_register_coalesced_mmio(kvm_context_t kvm, uint64_t addr, uint32_t size)
+int kvm_coalesce_mmio_region(target_phys_addr_t addr, ram_addr_t size)
 {
 #ifdef KVM_CAP_COALESCED_MMIO
+	kvm_context_t kvm = kvm_context;
 	struct kvm_coalesced_mmio_zone zone;
 	int r;
 
@@ -1126,9 +1127,10 @@ int kvm_register_coalesced_mmio(kvm_context_t kvm, uint64_t addr, uint32_t size)
 	return -ENOSYS;
 }
 
-int kvm_unregister_coalesced_mmio(kvm_context_t kvm, uint64_t addr, uint32_t size)
+int kvm_uncoalesce_mmio_region(target_phys_addr_t addr, ram_addr_t size)
 {
 #ifdef KVM_CAP_COALESCED_MMIO
+	kvm_context_t kvm = kvm_context;
 	struct kvm_coalesced_mmio_zone zone;
 	int r;
 
@@ -2759,27 +2761,6 @@ void kvm_mutex_lock(void)
     cpu_single_env = NULL;
 }
 
-int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr, unsigned int size)
-{
-    return kvm_register_coalesced_mmio(kvm_context, addr, size);
-}
-
-int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr,
-				       unsigned int size)
-{
-    return kvm_unregister_coalesced_mmio(kvm_context, addr, size);
-}
-
-int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
-{
-    return kvm_register_coalesced_mmio(kvm_context, start, size);
-}
-
-int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
-{
-    return kvm_unregister_coalesced_mmio(kvm_context, start, size);
-}
-
 #ifdef USE_KVM_DEVICE_ASSIGNMENT
 void kvm_add_ioperm_data(struct ioperm_data *data)
 {
qemu-kvm.h +0 -5
index de90259..0322feb 100644
@@ -116,11 +116,6 @@ void kvm_tpr_access_report(CPUState *env, uint64_t rip, int is_write);
 void kvm_tpr_vcpu_start(CPUState *env);
 
 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf);
-int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr,
-				     unsigned int size);
-int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr,
-				       unsigned int size);
-
 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
 
Signature · 1 line
1.6.2.2
#3 lore raw

PATCH v3 3/5 kvm finalize

---
 qemu-kvm.c |   11 +++--------
 qemu-kvm.h |    1 -
 2 files changed, 3 insertions(+), 9 deletions(-)
qemu-kvm.c +3 -8
index e4af96e..3b5326e 100644
@@ -2262,12 +2262,12 @@ int kvm_qemu_create_context(void)
         kvm_disable_pit_creation(kvm_context);
     }
     if (kvm_create(kvm_context, 0, NULL) < 0) {
-	kvm_qemu_destroy();
-	return -1;
+       kvm_finalize(kvm_context);
+       return -1;
     }
     r = kvm_arch_qemu_create_context();
     if(r <0)
-	kvm_qemu_destroy();
+       kvm_finalize(kvm_context);
     if (kvm_pit && !kvm_pit_reinject) {
         if (kvm_reinject_control(kvm_context, 0)) {
             fprintf(stderr, "failure to disable in-kernel PIT reinjection\n");
@@ -2286,11 +2286,6 @@ int kvm_qemu_create_context(void)
     return 0;
 }
 
-void kvm_qemu_destroy(void)
-{
-    kvm_finalize(kvm_context);
-}
-
 #ifdef TARGET_I386
 static int must_use_aliases_source(target_phys_addr_t addr)
 {
qemu-kvm.h +0 -1
index 0322feb..10f0a80 100644
@@ -19,7 +19,6 @@ int kvm_qemu_init(void);
 int kvm_qemu_create_context(void);
 int kvm_init_ap(void);
 int kvm_vcpu_inited(CPUState *env);
-void kvm_qemu_destroy(void);
 void kvm_load_registers(CPUState *env);
 void kvm_save_registers(CPUState *env);
 void kvm_load_mpstate(CPUState *env);
Signature · 1 line
1.6.2.2
#4 lore raw

PATCH v3 4/5 remove callbacks structure

The purpose of that was only to allow the user of libkvm to register functions pointers that corresponded to possible actions. We don't need that anymore.

---
 libkvm-all.h   |    4 +-
 qemu-kvm-x86.c |   16 +--
 qemu-kvm.c     |  370 ++++++++++++++++++++++++++------------------------------
 qemu-kvm.h     |    3 +
 4 files changed, 179 insertions(+), 214 deletions(-)
libkvm-all.h +1 -3
index f348e69..6964b7a 100644
@@ -175,12 +175,10 @@ struct kvm_callbacks {
  * holds information about the KVM instance that gets created by this call.\n
  * This should always be your first call to KVM.
  *
- * \param callbacks Pointer to a valid kvm_callbacks structure
  * \param opaque Not used
  * \return NULL on failure
  */
-kvm_context_t kvm_init(struct kvm_callbacks *callbacks,
-		       void *opaque);
+kvm_context_t kvm_init(void *opaque);
 
 /*!
  * \brief Cleanup the KVM context
qemu-kvm-x86.c +5 -11
index e528acb..0620c14 100644
@@ -121,9 +121,10 @@ int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,
 static int kvm_handle_tpr_access(kvm_vcpu_context_t vcpu)
 {
 	struct kvm_run *run = vcpu->run;
-	return vcpu->kvm->callbacks->tpr_access(vcpu->kvm->opaque, vcpu,
-					  run->tpr_access.rip,
-					  run->tpr_access.is_write);
+	kvm_tpr_access_report(cpu_single_env,
+                         run->tpr_access.rip,
+                         run->tpr_access.is_write);
+    return 0;
 }
 
 
@@ -326,7 +327,7 @@ void kvm_show_code(kvm_vcpu_context_t vcpu)
 	for (n = -back_offset; n < SHOW_CODE_LEN-back_offset; ++n) {
 		if (n == 0)
 			strcat(code_str, " -->");
-		r = kvm->callbacks->mmio_read(kvm->opaque, rip + n, &code, 1);
+		r = kvm_mmio_read(kvm->opaque, rip + n, &code, 1);
 		if (r < 0) {
 			strcat(code_str, " xx");
 			continue;
@@ -1344,13 +1345,6 @@ void kvm_arch_update_regs_for_sipi(CPUState *env)
     kvm_arch_load_regs(env);
 }
 
-int handle_tpr_access(void *opaque, kvm_vcpu_context_t vcpu,
-			     uint64_t rip, int is_write)
-{
-    kvm_tpr_access_report(cpu_single_env, rip, is_write);
-    return 0;
-}
-
 void kvm_arch_cpu_reset(CPUState *env)
 {
     kvm_arch_load_regs(env);
qemu-kvm.c +170 -200

500 lines not on this page · raw message

qemu-kvm.h +3 -0
index 10f0a80..87c84d6 100644
@@ -120,6 +120,9 @@ int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
 
 int kvm_arch_init_irq_routing(void);
 
+int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len);
+int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len);
+
 #ifdef USE_KVM_DEVICE_ASSIGNMENT
 struct ioperm_data;
 
Signature · 1 line
1.6.2.2
#5 lore raw

PATCH v3 5/5 remove wrappers around ex-libkvm functions

Some functions in qemu existed only to call libkvm counterparts. Merge some of them

---
 libkvm-all.h |    6 ++--
 qemu-kvm.c   |   75 ++++++++++++++--------------------------------------------
 2 files changed, 21 insertions(+), 60 deletions(-)
libkvm-all.h +3 -3
index 6964b7a..118425f 100644
@@ -98,9 +98,9 @@ int kvm_arch_run(kvm_vcpu_context_t vcpu);
 void kvm_show_code(kvm_vcpu_context_t vcpu);
 
 int handle_halt(kvm_vcpu_context_t vcpu);
-int handle_shutdown(kvm_context_t kvm, void *env);
-void post_kvm_run(kvm_context_t kvm, void *env);
-int pre_kvm_run(kvm_context_t kvm, void *env);
+int handle_shutdown(kvm_context_t kvm, CPUState *env);
+void post_kvm_run(kvm_context_t kvm, CPUState *env);
+int pre_kvm_run(kvm_context_t kvm, CPUState *env);
 int handle_io_window(kvm_context_t kvm);
 int handle_debug(kvm_vcpu_context_t vcpu, void *env);
 int try_push_interrupts(kvm_context_t kvm);
qemu-kvm.c +18 -57

111 lines not on this page · raw message

Signature · 1 line
1.6.2.2
Applied
#6 lore raw
In reply to Glauber Costa
Quoted 3 lines · Glauber Costa

Now that I've definitely flushed my backlog, here's a resend of the last cleanup series I sent. It fixes the segfault avi noticed.

Applied, thanks.

Signature · 1 line
error compiling committee.c: too many arguments to function