From: Jan Kiszka <jan.kiszka@xxxxxxxxxxx> A small step towards switching to upstream code: reduce the init function maze. Signed-off-by: Jan Kiszka <jan.kiszka@xxxxxxxxxxx> --- qemu-kvm.c | 94 +++++++++++++++++++++--------------------------------------- qemu-kvm.h | 17 ----------- 2 files changed, 33 insertions(+), 78 deletions(-) diff --git a/qemu-kvm.c b/qemu-kvm.c index e03b3d5..359bec7 100644 --- a/qemu-kvm.c +++ b/qemu-kvm.c @@ -127,7 +127,7 @@ static int kvm_create_context(void); int kvm_init(void) { int fd; - int r, gsi_count; + int r, gsi_count, i; fd = open("/dev/kvm", O_RDWR); @@ -162,6 +162,21 @@ int kvm_init(void) QTAILQ_INIT(&kvm_state->kvm_sw_breakpoints); #endif + for (i = 0; i < ARRAY_SIZE(kvm_state->slots); i++) { + kvm_state->slots[i].slot = i; + } + +#ifdef KVM_CAP_USER_MEMORY + r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_USER_MEMORY); + if (r <= 0) { + fprintf(stderr, + "Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported\n"); + goto out_close; + } +#else +#error Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported +#endif + gsi_count = kvm_get_gsi_count(kvm_context); if (gsi_count > 0) { int gsi_bits, i; @@ -212,39 +227,6 @@ static int kvm_set_boot_vcpu_id(kvm_context_t kvm, uint32_t id) #endif } -int kvm_create_vm(kvm_context_t kvm) -{ - int fd; -#ifdef KVM_CAP_IRQ_ROUTING - kvm->irq_routes = qemu_mallocz(sizeof(*kvm->irq_routes)); - kvm->nr_allocated_irq_routes = 0; -#endif - - fd = kvm_ioctl(kvm_state, KVM_CREATE_VM, 0); - if (fd < 0) { - fprintf(stderr, "kvm_create_vm: %m\n"); - return -1; - } - kvm_state->vmfd = fd; - return 0; -} - -static int kvm_create_default_phys_mem(kvm_context_t kvm, - unsigned long phys_mem_bytes, - void **vm_mem) -{ -#ifdef KVM_CAP_USER_MEMORY - int r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_USER_MEMORY); - if (r > 0) - return 0; - fprintf(stderr, - "Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported\n"); -#else -#error Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported -#endif - return -1; -} - void kvm_create_irqchip(kvm_context_t kvm) { int r; @@ -271,32 +253,6 @@ void kvm_create_irqchip(kvm_context_t kvm) #endif } -int kvm_create(kvm_context_t kvm, unsigned long phys_mem_bytes, void **vm_mem) -{ - int r, i; - - r = kvm_create_vm(kvm); - if (r < 0) { - return r; - } - r = kvm_arch_create(kvm, phys_mem_bytes, vm_mem); - if (r < 0) { - return r; - } - for (i = 0; i < ARRAY_SIZE(kvm_state->slots); i++) { - kvm_state->slots[i].slot = i; - } - - r = kvm_create_default_phys_mem(kvm, phys_mem_bytes, vm_mem); - if (r < 0) { - return r; - } - - kvm_create_irqchip(kvm); - - return 0; -} - #ifdef KVM_CAP_IRQCHIP int kvm_set_irq_level(kvm_context_t kvm, int irq, int level, int *status) @@ -1429,10 +1385,26 @@ static int kvm_create_context(void) kvm_state->pit_in_kernel = kvm_pit; - if (kvm_create(kvm_context, 0, NULL) < 0) { +#ifdef KVM_CAP_IRQ_ROUTING + kvm_context->irq_routes = qemu_mallocz(sizeof(*kvm_context->irq_routes)); + kvm_context->nr_allocated_irq_routes = 0; +#endif + + kvm_state->vmfd = kvm_ioctl(kvm_state, KVM_CREATE_VM, 0); + if (kvm_state->vmfd < 0) { + fprintf(stderr, "kvm_create_vm: %m\n"); kvm_finalize(kvm_state); return -1; } + + r = kvm_arch_create(kvm_context, 0, NULL); + if (r < 0) { + kvm_finalize(kvm_state); + return r; + } + + kvm_create_irqchip(kvm_context); + r = kvm_arch_qemu_create_context(); if (r < 0) { kvm_finalize(kvm_state); diff --git a/qemu-kvm.h b/qemu-kvm.h index 409809e..a4f1816 100644 --- a/qemu-kvm.h +++ b/qemu-kvm.h @@ -80,23 +80,6 @@ int pre_kvm_run(kvm_context_t kvm, CPUState *env); int handle_io_window(kvm_context_t kvm); int try_push_interrupts(kvm_context_t kvm); -/*! - * \brief Create new virtual machine - * - * This creates a new virtual machine, maps physical RAM to it, and creates a - * virtual CPU for it.\n - * \n - * Memory gets mapped for addresses 0->0xA0000, 0xC0000->phys_mem_bytes - * - * \param kvm Pointer to the current kvm_context - * \param phys_mem_bytes The amount of physical ram you want the VM to have - * \param phys_mem This pointer will be set to point to the memory that - * kvm_create allocates for physical RAM - * \return 0 on success - */ -int kvm_create(kvm_context_t kvm, unsigned long phys_mem_bytes, - void **phys_mem); -int kvm_create_vm(kvm_context_t kvm); void kvm_create_irqchip(kvm_context_t kvm); /*! -- 1.7.1 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html