From patchwork Wed Jul 8 13:08:59 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glauber Costa X-Patchwork-Id: 34612 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n68D9Fad016568 for ; Wed, 8 Jul 2009 13:09:16 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754768AbZGHNJL (ORCPT ); Wed, 8 Jul 2009 09:09:11 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1754589AbZGHNJK (ORCPT ); Wed, 8 Jul 2009 09:09:10 -0400 Received: from mx2.redhat.com ([66.187.237.31]:38646 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754149AbZGHNJH (ORCPT ); Wed, 8 Jul 2009 09:09:07 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n68D97SV026586 for ; Wed, 8 Jul 2009 09:09:07 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n68D96sZ024284; Wed, 8 Jul 2009 09:09:06 -0400 Received: from localhost.localdomain (virtlab1.virt.bos.redhat.com [10.16.72.21]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n68D92uI029245; Wed, 8 Jul 2009 09:09:05 -0400 From: Glauber Costa 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 Message-Id: <1247058542-31211-6-git-send-email-glommer@redhat.com> In-Reply-To: <1247058542-31211-5-git-send-email-glommer@redhat.com> References: <1247058542-31211-1-git-send-email-glommer@redhat.com> <1247058542-31211-2-git-send-email-glommer@redhat.com> <1247058542-31211-3-git-send-email-glommer@redhat.com> <1247058542-31211-4-git-send-email-glommer@redhat.com> <1247058542-31211-5-git-send-email-glommer@redhat.com> X-Scanned-By: MIMEDefang 2.58 on 172.16.27.26 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 --- 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 + /* 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