From patchwork Mon Jun 8 19:10:24 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glauber Costa X-Patchwork-Id: 28688 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 n58JAbnC019633 for ; Mon, 8 Jun 2009 19:10:37 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751969AbZFHTK3 (ORCPT ); Mon, 8 Jun 2009 15:10:29 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1751638AbZFHTK3 (ORCPT ); Mon, 8 Jun 2009 15:10:29 -0400 Received: from mx2.redhat.com ([66.187.237.31]:49070 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751386AbZFHTKZ (ORCPT ); Mon, 8 Jun 2009 15:10:25 -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 n58JASxH025658 for ; Mon, 8 Jun 2009 15:10:28 -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 n58JARY1025715; Mon, 8 Jun 2009 15:10:27 -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 n58JAPS6029961; Mon, 8 Jun 2009 15:10:26 -0400 From: Glauber Costa To: kvm@vger.kernel.org Cc: avi@redhat.com Subject: [PATCH 4/4] Use KVMState as main state container Date: Mon, 8 Jun 2009 15:10:24 -0400 Message-Id: <1244488224-31171-5-git-send-email-glommer@redhat.com> In-Reply-To: <1244488224-31171-4-git-send-email-glommer@redhat.com> References: <1244488224-31171-1-git-send-email-glommer@redhat.com> <1244488224-31171-2-git-send-email-glommer@redhat.com> <1244488224-31171-3-git-send-email-glommer@redhat.com> <1244488224-31171-4-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 Put kvm_context inside KVMState. We can then start using KVMState where we need to, to mid-term, start sharing code with qemu mainline. Signed-off-by: Glauber Costa --- kvm.h | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- libkvm-all.c | 19 ++++++++++--------- libkvm-all.h | 47 +---------------------------------------------- qemu-kvm.c | 9 +++++---- 4 files changed, 68 insertions(+), 62 deletions(-) diff --git a/kvm.h b/kvm.h index 553c03e..32af545 100644 --- a/kvm.h +++ b/kvm.h @@ -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, ...); diff --git a/libkvm-all.c b/libkvm-all.c index 45679fb..32fdf96 100644 --- a/libkvm-all.c +++ b/libkvm-all.c @@ -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) diff --git a/libkvm-all.h b/libkvm-all.h index d647ef1..251dede 100644 --- a/libkvm-all.h +++ b/libkvm-all.h @@ -36,51 +36,6 @@ /* kvm abi verison variable */ extern int kvm_abi; -/** - * \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; #include "kvm.h" int kvm_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory, @@ -181,7 +136,7 @@ struct kvm_callbacks { * \param opaque Not used * \return NULL on failure */ -kvm_context_t kvm_init(struct kvm_callbacks *callbacks, +KVMState *kvm_init(struct kvm_callbacks *callbacks, void *opaque); /*! diff --git a/qemu-kvm.c b/qemu-kvm.c index 2aeb17c..b441970 100644 --- a/qemu-kvm.c +++ b/qemu-kvm.c @@ -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;