Skip to contentSkip to conversation

kvm tools: Introduce KVM VCPU data structure

PATCH In review Pekka Enberg 3 participants 11 messages Apr 9, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm tools: Introduce KVM VCPU data structure

In preparation for threaded execution model, this patch introduces a KVM VCPU data structure 'struct kvm_cpu'.

Cc Asias He <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 tools/kvm/Makefile              |    1 +
 tools/kvm/cpuid.c               |    6 +-
 tools/kvm/include/kvm/kvm-cpu.h |   33 ++++
 tools/kvm/include/kvm/kvm.h     |   16 --
 tools/kvm/ioport.c              |    2 +
 tools/kvm/kvm-cpu.c             |  370 +++++++++++++++++++++++++++++++++++++++
 tools/kvm/kvm-run.c             |   62 ++++---
 tools/kvm/kvm.c                 |  338 -----------------------------------
 8 files changed, 445 insertions(+), 383 deletions(-)
 create mode 100644 tools/kvm/include/kvm/kvm-cpu.h
 create mode 100644 tools/kvm/kvm-cpu.c
tools/kvm/Makefile +1 -0
index 4bcfd74..141cdec 100644
@@ -21,6 +21,7 @@ OBJS	+= disk-image.o
 OBJS	+= interrupt.o
 OBJS	+= ioport.o
 OBJS	+= kvm.o
+OBJS	+= kvm-cpu.o
 OBJS	+= main.o
 OBJS	+= mmio.o
 OBJS	+= pci.o
tools/kvm/cpuid.c +4 -2
index f7cc930..0b26eb1 100644
@@ -1,3 +1,5 @@
+#include "kvm/kvm-cpu.h"
+
 #include "kvm/kvm.h"
 #include "kvm/util.h"
 
@@ -30,14 +32,14 @@ static void filter_cpuid(struct kvm_cpuid2 *kvm_cpuid)
 	}
 }
 
-void kvm__setup_cpuid(struct kvm *self)
+void kvm_cpu__setup_cpuid(struct kvm_cpu *self)
 {
 	struct kvm_cpuid2 *kvm_cpuid;
 
 	kvm_cpuid = calloc(1, sizeof(*kvm_cpuid) + MAX_KVM_CPUID_ENTRIES * sizeof(*kvm_cpuid->entries));
 
 	kvm_cpuid->nent = MAX_KVM_CPUID_ENTRIES;
-	if (ioctl(self->sys_fd, KVM_GET_SUPPORTED_CPUID, kvm_cpuid) < 0)
+	if (ioctl(self->kvm->sys_fd, KVM_GET_SUPPORTED_CPUID, kvm_cpuid) < 0)
 		die_perror("KVM_GET_SUPPORTED_CPUID failed");
 
 	filter_cpuid(kvm_cpuid);
tools/kvm/include/kvm/kvm-cpu.h new file +33 -0
new file mode 100644
index 0000000..d36dadf
@@ -0,0 +1,33 @@
+#ifndef KVM__KVM_CPU_H
+#define KVM__KVM_CPU_H
+
+#include <linux/kvm.h>	/* for struct kvm_regs */
+
+#include <stdint.h>
+
+struct kvm;
+
+struct kvm_cpu {
+	struct kvm		*kvm;		/* parent KVM */
+	int			vcpu_fd;	/* For VCPU ioctls() */
+	struct kvm_run		*kvm_run;
+
+	struct kvm_regs		regs;
+	struct kvm_sregs	sregs;
+	struct kvm_fpu		fpu;
+
+	struct kvm_msrs		*msrs;		/* dynamically allocated */
+};
+
+struct kvm_cpu *kvm_cpu__init(struct kvm *kvm);
+void kvm_cpu__delete(struct kvm_cpu *self);
+void kvm_cpu__reset_vcpu(struct kvm_cpu *self);
+void kvm_cpu__setup_cpuid(struct kvm_cpu *self);
+void kvm_cpu__enable_singlestep(struct kvm_cpu *self);
+void kvm_cpu__run(struct kvm_cpu *self);
+
+void kvm_cpu__show_code(struct kvm_cpu *self);
+void kvm_cpu__show_registers(struct kvm_cpu *self);
+void kvm_cpu__show_page_tables(struct kvm_cpu *self);
+
+#endif /* KVM__KVM_CPU_H */
tools/kvm/include/kvm/kvm.h +0 -16
index a099307..7af98f9 100644
@@ -3,8 +3,6 @@
 
 #include "kvm/interrupt.h"
 
-#include <linux/kvm.h>	/* for struct kvm_regs */
-
 #include <stdbool.h>
 #include <stdint.h>
 #include <time.h>
@@ -12,9 +10,7 @@
 struct kvm {
 	int			sys_fd;		/* For system ioctls(), i.e. /dev/kvm */
 	int			vm_fd;		/* For VM ioctls() */
-	int			vcpu_fd;	/* For VCPU ioctls() */
 	timer_t			timerid;	/* Posix timer for interrupts */
-	struct kvm_run		*kvm_run;
 
 	struct disk_image	*disk_image;
 	uint64_t		ram_size;
@@ -26,25 +22,16 @@ struct kvm {
 	uint16_t		boot_ip;
 	uint16_t		boot_sp;
 
-	struct kvm_regs		regs;
-	struct kvm_sregs	sregs;
-	struct kvm_fpu		fpu;
-	struct kvm_msrs		*msrs;	/* dynamically allocated */
-
 	struct interrupt_table	interrupt_table;
 };
 
 struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size);
 void kvm__delete(struct kvm *self);
-void kvm__setup_cpuid(struct kvm *self);
-void kvm__enable_singlestep(struct kvm *self);
 bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename,
 			const char *initrd_filename, const char *kernel_cmdline);
-void kvm__reset_vcpu(struct kvm *self);
 void kvm__setup_bios(struct kvm *self);
 void kvm__start_timer(struct kvm *self);
 void kvm__stop_timer(struct kvm *self);
-void kvm__run(struct kvm *self);
 void kvm__irq_line(struct kvm *self, int irq, int level);
 bool kvm__emulate_io(struct kvm *self, uint16_t port, void *data, int direction, int size, uint32_t count);
 bool kvm__emulate_mmio(struct kvm *self, uint64_t phys_addr, uint8_t *data, uint32_t len, uint8_t is_write);
@@ -52,9 +39,6 @@ bool kvm__emulate_mmio(struct kvm *self, uint64_t phys_addr, uint8_t *data, uint
 /*
  * Debugging
  */
-void kvm__show_code(struct kvm *self);
-void kvm__show_registers(struct kvm *self);
-void kvm__show_page_tables(struct kvm *self);
 void kvm__dump_mem(struct kvm *self, unsigned long addr, unsigned long size);
 
 extern const char *kvm_exit_reasons[];
tools/kvm/ioport.c +2 -0
index 4579e89..6303571 100644
@@ -2,6 +2,8 @@
 
 #include "kvm/kvm.h"
 
+#include <linux/kvm.h>	/* for KVM_EXIT_* */
+
 #include <stdbool.h>
 #include <assert.h>
 #include <limits.h>
tools/kvm/kvm-cpu.c new file +370 -0

370 lines not on this page · raw message

tools/kvm/kvm-run.c +35 -27

130 lines not on this page · raw message

tools/kvm/kvm.c +0 -338

377 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 7 lines · Pekka Enberg

In preparation for threaded execution model, this patch introduces a KVM VCPU data structure 'struct kvm_cpu'.

Cc Asias He <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Ingo Molnar <[email protected]>
Quoted 12 lines
---
 tools/kvm/Makefile              |    1 +
 tools/kvm/cpuid.c               |    6 +-
 tools/kvm/include/kvm/kvm-cpu.h |   33 ++++
 tools/kvm/include/kvm/kvm.h     |   16 --
 tools/kvm/ioport.c              |    2 +
 tools/kvm/kvm-cpu.c             |  370 +++++++++++++++++++++++++++++++++++++++
 tools/kvm/kvm-run.c             |   62 ++++---
 tools/kvm/kvm.c                 |  338 -----------------------------------
 8 files changed, 445 insertions(+), 383 deletions(-)
 create mode 100644 tools/kvm/include/kvm/kvm-cpu.h
 create mode 100644 tools/kvm/kvm-cpu.c

This commit causes a segfault for 'kvm run bzImage':

(gdb) set args run bzImage
(gdb) run
Starting program: /home/mingo/tip/tools/kvm/kvm run bzImage [Thread debugging using libthread_db enabled]

Program received signal SIGSEGV, Segmentation fault.
0x0000000000405054 in setup_bios (kvm=0x68a010) at bios.c:106
106 memset(p, 0, BDA_END - BDA_START);
Missing separate debuginfos, use: debuginfo-install glibc-2.13-1.x86_64
(gdb) bt
#0 0x0000000000405054 in setup_bios (kvm=0x68a010) at bios.c:106
#1 0x0000000000404ef5 in kvm_cmd_run (argc=<value optimized out>, argv=<value optimized out>,
prefix=<value optimized out>) at kvm-run.c:160
#2 0x00000000004038cf in handle_kvm_command (argc=<value optimized out>, argv=<value optimized out>)

    at main.c:16

#3 main (argc=<value optimized out>, argv=<value optimized out>) at main.c:21 (gdb) q
A debugging session is active.

Thanks,

Ingo

#2 lore raw
Quoted 11 lines · Ingo Molnar
Quoted 8 lines · Pekka Enberg

In preparation for threaded execution model, this patch introduces a KVM VCPU data structure 'struct kvm_cpu'.

Cc Asias He <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Ingo Molnar <[email protected]>

---

...

Quoted 11 lines

This commit causes a segfault for 'kvm run bzImage':

(gdb) set args run bzImage
(gdb) run
Starting program: /home/mingo/tip/tools/kvm/kvm run bzImage [Thread debugging using libthread_db enabled]

Program received signal SIGSEGV, Segmentation fault. 0x0000000000405054 in setup_bios (kvm=0x68a010) at bios.c:106
106 memset(p, 0, BDA_END - BDA_START);

Ugh, Pekka, was the memory properly mmaped? Setup bios called multiple times in every thread?

Signature · 1 line
    Cyrill
#3 lore raw
Quoted 21 lines · Cyrill Gorcunov
Quoted 11 lines · Ingo Molnar
Quoted 8 lines · Pekka Enberg

In preparation for threaded execution model, this patch introduces a KVM VCPU data structure 'struct kvm_cpu'.

Cc Asias He <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Ingo Molnar <[email protected]>

---

...

Quoted 8 lines

This commit causes a segfault for 'kvm run bzImage':

(gdb) set args run bzImage
(gdb) run
Starting program: /home/mingo/tip/tools/kvm/kvm run bzImage [Thread debugging using libthread_db enabled]

Quoted 6 lines
Quoted 3 lines

Program received signal SIGSEGV, Segmentation fault. 0x0000000000405054 in setup_bios (kvm=0x68a010) at bios.c:106
106 memset(p, 0, BDA_END - BDA_START);

Ugh, Pekka, was the memory properly mmaped? Setup bios called multiple times in every thread?

Why should mmap() be called in every thread?

Thanks,

Ingo

#4 lore raw

On 04/09/2011 04:27 PM, Ingo Molnar wrote:
...

Quoted 9 lines
Quoted 3 lines

Ugh, Pekka, was the memory properly mmaped? Setup bios called multiple times in every thread?

Why should mmap() be called in every thread?

Thanks,

Ingo

I thought it being called for some reason.
I'm not updated to this threaded model yet.

Signature · 1 line
    Cyrill
#5 lore raw
Quoted 2 lines · Cyrill Gorcunov

I thought it being called for some reason.
I'm not updated to this threaded model yet.

None of the fancy stuff is merged. It's likely just because of struct layout changes combined with broken header dep checking.

Pekka

#6 lore raw
Quoted 7 lines · Pekka Enberg
Quoted 2 lines · Cyrill Gorcunov

I thought it being called for some reason.
I'm not updated to this threaded model yet.

None of the fancy stuff is merged. It's likely just because of struct layout changes combined with broken header dep checking.

Pekka

ah, ok, this might explain. While the sources are small, maybe we should call make clean all the time?

Signature · 1 line
    Cyrill
#7 lore raw
Quoted 1 line · Cyrill Gorcunov

ah, ok, this might explain. While the sources are small, maybe we should call make clean all the time?

We should just fix the Makefile. :-)

#8 lore raw
In reply to Ingo Molnar
Quoted 1 line · Ingo Molnar

This commit causes a segfault for 'kvm run bzImage':

Weird - i tried to debug it, then the bug went away. I'm using the same sha1 now as before, but the crash does not trigger. Very weird.

But this definitely started triggering today and never triggered before, so it's something recent.

Tanks,

Ingo

#9 lore raw
Quoted 5 lines · Ingo Molnar

Weird - i tried to debug it, then the bug went away. I'm using the same sha1 now as before, but the crash does not trigger. Very weird.

But this definitely started triggering today and never triggered before, so it's something recent.

Let me guess, you did 'make clean'? I think our header dep checking might be busted in the Makefile...

Pekka

#10 lore raw
Quoted 9 lines · Pekka Enberg
Quoted 5 lines · Ingo Molnar

Weird - i tried to debug it, then the bug went away. I'm using the same sha1 now as before, but the crash does not trigger. Very weird.

But this definitely started triggering today and never triggered before, so it's something recent.

Let me guess, you did 'make clean'? I think our header dep checking might be busted in the Makefile...

That indeed could be it.

Thanks,

Ingo