Start using KVMState
cover Start using KVMState
Guys,
This is to address the feedback I received in the last attempt.
I'm now attempting to move kvm_context inside of KVMState Patch #3 should be applied to qemu. I'll relay it to Anthony if we reach consensus on it here.
Patch #4 is an example use of KVMState, to show how I intend to proceed.
Patches 1 and 2 are cleanups that can be applied separatedly if we want to.
PATCH 1/4 get rid of libkvm-common.h
This file has no purpose in life in new scheme of things. Move its contents to libkvm-all.h. There are some name clashes in qemu-kvm.c. Those function names are then changed.
--- kvm.h | 1 + libkvm-all.h | 82 ++++++++++++++++++++++++++++++++++++++++- libkvm-common.h | 100 -------------------------------------------------- qemu-kvm-x86.c | 2 + qemu-kvm.c | 12 +++--- target-i386/libkvm.h | 2 +- target-ia64/libkvm.h | 2 +- target-ppc/libkvm.h | 2 +- 8 files changed, 92 insertions(+), 111 deletions(-) delete mode 100644 libkvm-common.h
kvm.h +1 -0
@@ -16,6 +16,7 @@#include "config.h" #include "sys-queue.h" +#include "libkvm-all.h" #ifdef KVM_UPSTREAM
libkvm-all.h +80 -2
92 lines not on this page · raw message
libkvm-common.h deleted +0 -100
100 lines not on this page · raw message
qemu-kvm-x86.c +2 -0
@@ -20,6 +20,8 @@#include <sys/utsname.h> #include <linux/kvm_para.h> +#include "kvm.h" +#define MSR_IA32_TSC 0x10static struct kvm_msr_list *kvm_msr_list;
qemu-kvm.c +6 -6
@@ -149,12 +149,12 @@ void kvm_apic_init(CPUState *env)#include <signal.h> -static int try_push_interrupts (void *opaque) +static int kvm_try_push_interrupts (void *opaque) {return kvm_arch_try_push_interrupts (opaque); } -static void post_kvm_run (void *opaque,void *data) +static void kvm_post_run (void *opaque,void *data) {CPUState *env = (CPUState *)data;
@@ -162,7 +162,7 @@ static void post_kvm_run(void *opaque, void *data)kvm_arch_post_kvm_run (opaque, env); } -static int pre_kvm_run (void *opaque,void *data) +static int kvm_pre_run (void *opaque,void *data) {CPUState *env = (CPUState *)data;
@@ -761,12 +761,12 @@ static struct kvm_callbacks qemu_kvm_ops = { .halt = kvm_halt, .shutdown = kvm_shutdown, .io_window = kvm_io_window, - .try_push_interrupts = try_push_interrupts, + .try_push_interrupts = kvm_try_push_interrupts, #ifdef KVM_CAP_USER_NMI .push_nmi = kvm_arch_push_nmi, #endif - .post_kvm_run = post_kvm_run, - .pre_kvm_run = pre_kvm_run, + .post_kvm_run = kvm_post_run, + .pre_kvm_run = kvm_pre_run, #ifdef TARGET_I386 .tpr_access = handle_tpr_access, #endif
target-i386/libkvm.h +1 -1
@@ -18,7 +18,7 @@#ifndef KVM_X86_H #define KVM_X86_H -#include "libkvm-common.h" +#include "libkvm-all.h" #define PAGE_SIZE 4096ul#define PAGE_MASK (~(PAGE_SIZE - 1))
target-ia64/libkvm.h +1 -1
@@ -18,7 +18,7 @@#ifndef KVM_IA64_H #define KVM_IA64_H -#include "libkvm-common.h" +#include "libkvm-all.h" extern int kvm_page_size;
target-ppc/libkvm.h +1 -1
@@ -19,7 +19,7 @@#ifndef KVM_POWERPC_H #define KVM_POWERPC_H -#include "libkvm-common.h" +#include "libkvm-all.h" extern int kvm_page_size;
Signature · 1 line
1.5.6.6
PATCH 2/4 pull qemu headers into libkvm
Those headers define qemu specific things like ram_addr_t. This will allow us to start using them in libkvm.
--- libkvm-all.c | 2 +- libkvm-all.h | 1 + target-i386/libkvm.c | 2 ++ 3 files changed, 4 insertions(+), 1 deletions(-)
libkvm-all.c +1 -1
@@ -26,6 +26,7 @@#error libkvm: userspace and kernel version mismatch#endif +#include "sysemu.h" #include <unistd.h> #include <fcntl.h> #include <stdio.h>
@@ -47,7 +48,6 @@ #define DPRINTF(fmt, args...) do {} while (0)#endif -#define MIN (x,y) ((x) < (y) ? (x) : (y))#define ALIGN (x, y) (((x)+(y)-1) & ~((y)-1))int kvm_abi =EXPECTED_KVM_API_VERSION ;
libkvm-all.h +1 -0
@@ -82,6 +82,7 @@ struct kvm_vcpu_contexttypedef struct kvm_context *kvm_context_t ;typedef struct kvm_vcpu_context *kvm_vcpu_context_t ; +#include "kvm.h" int kvm_alloc_kernel_memory (kvm_context_t kvm,unsigned long memory,void **vm_mem); int kvm_alloc_userspace_memory(kvm_context_t kvm, unsigned long memory,
target-i386/libkvm.c +2 -0
@@ -1,3 +1,5 @@ +#include "sysemu.h" +#include "libkvm-all.h" #include "libkvm.h" #include <errno.h>
Signature · 1 line
1.5.6.6
PATCH 3/4 Move KVMState to common header
This patch should be applied to main qemu, but I'll first post it here for appreciation. In this patch, we move KVMState definition to kvm.h header. With this done, we can also use its definition in our files, until there is no more such thing as "our" files. This is too selfish anyway.
Later on, we'll move our internal state inside it.
--- kvm-all.c | 21 --------------------- kvm.h | 40 +++++++++++++++++++++++++++++++--------- qemu-kvm.h | 8 -------- 3 files changed, 31 insertions(+), 38 deletions(-)
kvm-all.c +0 -21
@@ -39,32 +39,11 @@ do { } while (0)#endif -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 ;int kvm_allowed =0 ; -struct KVMState -{ -KVMSlot slots[32 ]; -int fd; -int vmfd; -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 -}; -static KVMState *kvm_state;static KVMSlot *kvm_alloc_slot (KVMState *s)
kvm.h +31 -9
@@ -18,6 +18,37 @@#include "sys-queue.h" #include "libkvm-all.h" +typedef struct KVMSlot +{ +target_phys_addr_t start_addr; +ram_addr_t memory_size; +ram_addr_t phys_offset; +int slot; +int flags; +}KVMSlot ; + +struct kvm_sw_breakpoint { +target_ulong pc; +target_ulong saved_insn; +int use_count; +TAILQ_ENTRY (kvm_sw_breakpoint) entry; +}; + +TAILQ_HEAD (kvm_sw_breakpoint_head, kvm_sw_breakpoint); + +struct KVMState +{ +KVMSlot slots[32 ]; +int fd; +int vmfd; +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 +}; +#ifdef KVM_UPSTREAM #ifdef CONFIG_KVM
@@ -97,15 +128,6 @@ int kvm_arch_init_vcpu(CPUState *env);struct kvm_guest_debug ;struct kvm_debug_exit_arch ; -struct kvm_sw_breakpoint { -target_ulong pc; -target_ulong saved_insn; -int use_count; -TAILQ_ENTRY (kvm_sw_breakpoint) entry; -}; - -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(CPUState *env,
qemu-kvm.h +0 -8
@@ -81,14 +81,6 @@ void kvm_arch_cpu_reset(CPUState *env);struct kvm_guest_debug ;struct kvm_debug_exit_arch ; -struct kvm_sw_breakpoint { -target_ulong pc; -target_ulong saved_insn; -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;int kvm_arch_debug (struct kvm_debug_exit_arch *arch_info);
Signature · 1 line
1.5.6.6
PATCH 4/4 Use KVMState as main state container
Put kvm_context inside KVMState. We can then start using KVMState where we need to, to mid-term, start sharing code with qemu mainline.
--- kvm.h | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- libkvm-all.c | 19 ++++++++++--------- libkvm-all.h | 47 +---------------------------------------------- qemu-kvm.c | 9 +++++---- 4 files changed, 68 insertions(+), 62 deletions(-)
kvm.h +52 -3
@@ -16,7 +16,53 @@#include "config.h" #include "sys-queue.h" -#include "libkvm-all.h" + +/** +* \brief The KVM context +* +* The verbose KVM context +*/ + +struct kvm_context { +/// Filedescriptor to /dev/kvm +int fd; +int vm_fd; +/// Callbacks that KVM uses to emulate various unvirtualizable functionality +struct kvm_callbacks *callbacks; +void *opaque; +/// is dirty pages logging enabled for all regions or not +int dirty_pages_log_all; +/// do not create in-kernel irqchip if set +int no_irqchip_creation; +/// in-kernel irqchip status +int irqchip_in_kernel; +/// ioctl to use to inject interrupts +int irqchip_inject_ioctl; +/// do not create in-kernel pit if set +int no_pit_creation; +/// in-kernel pit status +int pit_in_kernel; +/// in-kernel coalesced mmio +int coalesced_mmio; +#ifdef KVM_CAP_IRQ_ROUTING +struct kvm_irq_routing *irq_routes; +int nr_allocated_irq_routes; +#endif +void *used_gsi_bitmap; +int max_gsi; +}; + +struct kvm_vcpu_context +{ +int fd; +struct kvm_run *run; +struct kvm_context *kvm; +uint32_t id; +}; + +typedef struct kvm_context *kvm_context_t ; +typedef struct kvm_vcpu_context *kvm_vcpu_context_t ; + typedef struct KVMSlot {
@@ -47,8 +93,13 @@ struct KVMState#ifdef KVM_CAP_SET_GUEST_DEBUG struct kvm_sw_breakpoint_head kvm_sw_breakpoints;#endif +#ifdef USE_KVM +struct kvm_context kvm_context; +#endif }; +typedef struct KVMState KVMState ; +#ifdef KVM_UPSTREAM #ifdef CONFIG_KVM
@@ -97,8 +148,6 @@ int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap);/* internal API */ -struct KVMState ; -typedef struct KVMState KVMState ;int kvm_ioctl (KVMState *s,int type, ...);
libkvm-all.c +10 -9
@@ -288,11 +288,12 @@ int kvm_dirty_pages_log_reset(kvm_context_t kvm) } -kvm_context_t kvm_init (struct kvm_callbacks *callbacks, +KVMState *kvm_init (struct kvm_callbacks *callbacks,void *opaque) {int fd;kvm_context_t kvm; +KVMState *s;int r, gsi_count; fd =open ("/dev/kvm" ,O_RDWR );
@@ -319,10 +320,9 @@ kvm_context_t kvm_init(struct kvm_callbacks *callbacks, } kvm_abi = r; kvm_page_size =getpagesize (); - kvm =malloc (sizeof (*kvm)); -if (kvm ==NULL ) - goto out_close; -memset (kvm,0 ,sizeof (*kvm)); + s =qemu_malloc (sizeof (*s)); +memset (s,0 ,sizeof (*s)); + kvm = &s->kvm_context; kvm->fd = fd; kvm->vm_fd =-1 ; kvm->callbacks = callbacks;
@@ -348,10 +348,11 @@ kvm_context_t kvm_init(struct kvm_callbacks *callbacks,set_gsi (kvm, i); } -return kvm; - out_close: -close (fd); -return NULL ; +return s; + +out_close: +close (fd); +return NULL ; }void kvm_finalize (kvm_context_t kvm)
libkvm-all.h +1 -46
59 lines not on this page · raw message
qemu-kvm.c +5 -4
@@ -33,6 +33,8 @@ int kvm_irqchip = 1;int kvm_pit =1 ;int kvm_pit_reinject =1 ;int kvm_nested =0 ; +KVMState *kvm_state; +kvm_context_t kvm_context;pthread_mutex_t qemu_mutex =PTHREAD_MUTEX_INITIALIZER ;
@@ -780,10 +782,9 @@ static struct kvm_callbacks qemu_kvm_ops = {int kvm_qemu_init () {/* Try to initialize kvm */ - kvm_context =kvm_init (&qemu_kvm_ops, cpu_single_env); -if (!kvm_context) { -return -1 ; - } + kvm_state =kvm_init (&qemu_kvm_ops, cpu_single_env); + kvm_context = &kvm_state->kvm_context; +pthread_mutex_lock (&qemu_mutex);return 0 ;
Signature · 1 line
1.5.6.6
Re: [PATCH 3/4] Move KVMState to common header
In reply to Glauber CostaQuoted 8 lines · Glauber Costa
This patch should be applied to main qemu, but I'll first post it here for appreciation. In this patch, we move KVMState definition to
kvm.hheader. With this done, we can also use its definition in our files, until there is no more such thing as "our" files. This is too selfish anyway.Later on, we'll move our internal state inside it.
Well, in upstream no one outside kvm-all.c needs to (and likely should be allowed to) access fields from struct KVMState & KVMSlot directly. That avoids misuse outside the KVM layer and enforces KVM arch code to properly call into the generic layer.
But I see the problem for qemu-kvm's transition time, so let's try to find an intermediate solution until its code layout is aligned (I don't see any blockers for this). Suggestion: Replicate the relevant structures into a new, temporary header. If upstream may extend its original structures, this should from now on have happened *first* inside qemu-kvm, so no inconsistency can arise unless downstream messed it up already. At some point (hopefully not too far away), no user of that header will remain and we will be able to drop it again.
Jan
Re: [PATCH 3/4] Move KVMState to common header
Quoted 15 lines · Jan Kiszka
Quoted 8 lines · Glauber Costa
This patch should be applied to main qemu, but I'll first post it here for appreciation. In this patch, we move KVMState definition to
kvm.hheader. With this done, we can also use its definition in our files, until there is no more such thing as "our" files. This is too selfish anyway.Later on, we'll move our internal state inside it.
Well, in upstream no one outside
kvm-all.cneeds to (and likely should be allowed to) access fields from struct KVMState & KVMSlot directly. That avoids misuse outside the KVM layer and enforces KVM arch code to properly call into the generic layer.
Quoted 8 lines
But I see the problem for qemu-kvm's transition time, so let's try to find an intermediate solution until its code layout is aligned (I don't see any blockers for this). Suggestion: Replicate the relevant structures into a new, temporary header. If upstream may extend its original structures, this should from now on have happened *first* inside qemu-kvm, so no inconsistency can arise unless downstream messed it up already. At some point (hopefully not too far away), no user of that header will remain and we will be able to drop it again.
I'm fine with whatever anthony wants.
Quoted 3 lines
Jan
Re: [PATCH 3/4] Move KVMState to common header
Quoted 27 lines · Glauber Costa
Quoted 16 lines · Jan Kiszka
Quoted 9 lines · Glauber Costa
This patch should be applied to main qemu, but I'll first post it here for appreciation. In this patch, we move KVMState definition to
kvm.hheader. With this done, we can also use its definition in our files, until there is no more such thing as "our" files. This is too selfish anyway.Later on, we'll move our internal state inside it.
Well, in upstream no one outside
kvm-all.cneeds to (and likely should be allowed to) access fields from struct KVMState & KVMSlot directly. That avoids misuse outside the KVM layer and enforces KVM arch code to properly call into the generic layer.Quoted 9 lines
But I see the problem for qemu-kvm's transition time, so let's try to find an intermediate solution until its code layout is aligned (I don't see any blockers for this). Suggestion: Replicate the relevant structures into a new, temporary header. If upstream may extend its original structures, this should from now on have happened *first* inside qemu-kvm, so no inconsistency can arise unless downstream messed it up already. At some point (hopefully not too far away), no user of that header will remain and we will be able to drop it again.
Quoted 2 lines
I'm fine with whatever anthony wants.
I'm fine with either solution as they are both temporary measures...
Regards,
Anthony Liguori
Quoted 4 lines · Glauber Costa
Guys,
This is to address the feedback I received in the last attempt.
Does this fix the tpr patching regression? What was the problem?
It always helps to explicitly list changes and add a version number to patchsets, to help maintainers with limited memory capacity.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 10 lines · Avi Kivity
Quoted 4 lines · Glauber Costa
Guys,
This is to address the feedback I received in the last attempt.
Does this fix the tpr patching regression? What was the problem?
It always helps to explicitly list changes and add a version number to patchsets, to help maintainers with limited memory capacity.
No, forget about these one. The one you should apply is the one that moves libkvm into qemu-kvm for i386. Subj is [PATCH] get rid of target-i386/libkvm.c
There is no tpr patching regression, since you said you did not push my previous series (that removes the callbacks). It (get rid of ...) will just prevent it from happening whenever I send it again.