From: Glauber Costa <glommer@redhat.com>
To: kvm@vger.kernel.org
Cc: avi@redhat.com
Subject: [PATCH 5/8] move kvm_context inside KVMState
Date: Wed, 8 Jul 2009 09:08:59 -0400 [thread overview]
Message-ID: <1247058542-31211-6-git-send-email-glommer@redhat.com> (raw)
In-Reply-To: <1247058542-31211-5-git-send-email-glommer@redhat.com>
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
Signed-off-by: Glauber Costa <glommer@redhat.com>
---
kvm-all.c | 98 +++++++++++++++++++++++++--------------------------------
kvm.h | 3 +-
libkvm-all.h | 3 +-
3 files changed, 47 insertions(+), 57 deletions(-)
diff --git a/kvm-all.c b/kvm-all.c
index 034ae52..15bd429 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -35,23 +35,6 @@
#include "libkvm-all.h"
#include "libkvm.h"
-
-#ifdef CONFIG_KVM
-
-
-/* KVM uses PAGE_SIZE in it's definition of COALESCED_MMIO_MAX */
-#define PAGE_SIZE TARGET_PAGE_SIZE
-
-//#define DEBUG_KVM
-
-#ifdef DEBUG_KVM
-#define dprintf(fmt, ...) \
- do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
-#else
-#define dprintf(fmt, ...) \
- do { } while (0)
-#endif
-
typedef struct KVMSlot
{
target_phys_addr_t start_addr;
@@ -63,8 +46,6 @@ typedef struct KVMSlot
typedef struct kvm_dirty_log KVMDirtyLog;
-int kvm_allowed = 0;
-
struct KVMState
{
KVMSlot slots[32];
@@ -76,9 +57,28 @@ struct KVMState
#ifdef KVM_CAP_SET_GUEST_DEBUG
struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
#endif
+ struct kvm_context kvm_context;
};
static KVMState *kvm_state;
+kvm_context_t kvm_context;
+
+#ifdef CONFIG_KVM
+
+/* KVM uses PAGE_SIZE in it's definition of COALESCED_MMIO_MAX */
+#define PAGE_SIZE TARGET_PAGE_SIZE
+
+//#define DEBUG_KVM
+
+#ifdef DEBUG_KVM
+#define dprintf(fmt, ...) \
+ do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
+#else
+#define dprintf(fmt, ...) \
+ do { } while (0)
+#endif
+
+int kvm_allowed = 0;
static KVMSlot *kvm_alloc_slot(KVMState *s)
{
@@ -1065,7 +1065,6 @@ int kvm_irqchip = 1;
int kvm_pit = 1;
int kvm_pit_reinject = 1;
int kvm_nested = 0;
-kvm_context_t kvm_context;
pthread_mutex_t qemu_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t qemu_vcpu_cond = PTHREAD_COND_INITIALIZER;
@@ -1439,16 +1438,15 @@ int kvm_dirty_pages_log_reset(kvm_context_t kvm)
}
-kvm_context_t kvm_init(void *opaque)
+int kvm_qemu_init()
{
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) {
@@ -1469,35 +1467,37 @@ 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;
-
- gsi_count = kvm_get_gsi_count(kvm);
+ kvm_state = qemu_mallocz(sizeof(*kvm_state));
+ kvm_context = &kvm_state->kvm_context;
+ 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;
- out_close:
+ pthread_mutex_lock(&qemu_mutex);
+ return 0;
+out_close:
close(fd);
- return NULL;
+ return -1;
}
-void kvm_finalize(kvm_context_t kvm)
+void kvm_finalize(KVMState *kvm_state)
{
/* FIXME
if (kvm->vcpu_fd[0] != -1)
@@ -1505,8 +1505,8 @@ void kvm_finalize(kvm_context_t kvm)
if (kvm->vm_fd != -1)
close(kvm->vm_fd);
*/
- close(kvm->fd);
- free(kvm);
+ close(kvm_state->kvm_context.fd);
+ free(kvm_state);
}
void kvm_disable_irqchip_creation(kvm_context_t kvm)
@@ -3220,18 +3220,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
@@ -3255,12 +3243,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");
diff --git a/kvm.h b/kvm.h
index 7648c49..31ebce2 100644
--- a/kvm.h
+++ b/kvm.h
@@ -16,11 +16,12 @@
#include "config.h"
#include "sys-queue.h"
-#include "libkvm-all.h"
struct KVMState;
typedef struct KVMState KVMState;
+#include "libkvm-all.h"
+
#ifdef KVM_UPSTREAM
#ifdef CONFIG_KVM
diff --git a/libkvm-all.h b/libkvm-all.h
index e16646c..7857532 100644
--- a/libkvm-all.h
+++ b/libkvm-all.h
@@ -21,6 +21,7 @@
#include <signal.h>
+
/* FIXME: share this number with kvm */
/* FIXME: or dynamically alloc/realloc regions */
#ifdef __s390__
@@ -189,7 +190,7 @@ kvm_context_t kvm_init(void *opaque);
*
* \param kvm Pointer to the kvm_context that is to be freed
*/
-void kvm_finalize(kvm_context_t kvm);
+void kvm_finalize(KVMState *s);
/*!
* \brief Disable the in-kernel IRQCHIP creation
--
1.6.2.2
next prev parent reply other threads:[~2009-07-08 13:09 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-07-08 13:08 [PATCH 0/8] Move closer to upstream Glauber Costa
2009-07-08 13:08 ` [PATCH 1/8] Provide KVMState definition outside CONFIG_KVM Glauber Costa
2009-07-08 13:22 ` Avi Kivity
[not found] ` <1247058542-31211-3-git-send-email-glommer@redhat.com>
2009-07-08 13:08 ` [PATCH 3/8] put qemu-kvm-x86.c into kvm.c Glauber Costa
2009-07-08 13:08 ` [PATCH 4/8] replace malloc with qemu_malloc Glauber Costa
2009-07-08 13:08 ` Glauber Costa [this message]
2009-07-08 13:09 ` [PATCH 6/8] provide env->kvm_fd Glauber Costa
2009-07-08 13:09 ` [PATCH 7/8] use kvm_upstream sw_breakpoints structure Glauber Costa
2009-07-08 13:09 ` [PATCH 8/8] use upstream code for breakpoint handling Glauber Costa
2009-07-08 13:27 ` [PATCH 7/8] use kvm_upstream sw_breakpoints structure Avi Kivity
2009-07-08 13:39 ` Glauber Costa
2009-07-08 13:44 ` Avi Kivity
2009-07-08 15:23 ` Jan Kiszka
2009-07-08 19:44 ` Jan Kiszka
2009-07-08 19:55 ` Avi Kivity
2009-07-08 13:32 ` [PATCH 6/8] provide env->kvm_fd Gleb Natapov
2009-07-08 13:44 ` Glauber Costa
2009-07-08 13:38 ` Gleb Natapov
2009-07-08 13:23 ` [PATCH 3/8] put qemu-kvm-x86.c into kvm.c Avi Kivity
2009-07-08 13:28 ` Gleb Natapov
2009-07-08 13:41 ` Glauber Costa
2009-07-08 13:23 ` [PATCH 2/8] move qemu-kvm.c to kvm-all.c Avi Kivity
2009-07-08 13:52 ` Glauber Costa
2009-07-08 13:27 ` [PATCH 0/8] Move closer to upstream Avi Kivity
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1247058542-31211-6-git-send-email-glommer@redhat.com \
--to=glommer@redhat.com \
--cc=avi@redhat.com \
--cc=kvm@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).