@@ -84,6 +84,7 @@ OBJS += net/uip/buf.o
OBJS += net/uip/csum.o
OBJS += net/uip/dhcp.o
OBJS += kvm-cmd.o
+OBJS += util/init.o
OBJS += util/rbtree.o
OBJS += util/threadpool.o
OBJS += util/parse-options.o
@@ -476,7 +476,6 @@ static int kvm_cmd_run_init(int argc, const char **argv)
{
static char real_cmdline[2048], default_name[20];
unsigned int nr_online_cpus;
- int r;
kvm = kvm__new();
if (IS_ERR(kvm))
@@ -646,160 +645,7 @@ static int kvm_cmd_run_init(int argc, const char **argv)
printf(" # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME,
kvm->cfg.kernel_filename, kvm->cfg.ram_size / 1024 / 1024, kvm->cfg.nrcpus, kvm->cfg.guest_name);
- r = kvm__init(kvm);
- if (r)
- goto fail;
-
- r = term_init(kvm);
- if (r < 0) {
- pr_err("term_init() failed with error %d\n", r);
- goto fail;
- }
-
-
- r = ioeventfd__init(kvm);
- if (r < 0) {
- pr_err("ioeventfd__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = kvm_cpu__init(kvm);
- if (r < 0) {
- pr_err("kvm_cpu__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = irq__init(kvm);
- if (r < 0) {
- pr_err("irq__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = pci__init(kvm);
- if (r < 0) {
- pr_err("pci__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = ioport__init(kvm);
- if (r < 0) {
- pr_err("ioport__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = disk_image__init(kvm);
- if (r < 0) {
- pr_err("disk_image__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = symbol_init(kvm);
- if (r < 0)
- pr_debug("symbol_init() failed with error %d\n", r);
-
- r = rtc__init(kvm);
- if (r < 0) {
- pr_err("rtc__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = serial8250__init(kvm);
- if (r < 0) {
- pr_err("serial__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = virtio_blk__init(kvm);
- if (r < 0) {
- pr_err("virtio_blk__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = virtio_scsi_init(kvm);
- if (r < 0) {
- pr_err("virtio_scsi_init() failed with error %d\n", r);
- goto fail;
- }
-
- r = virtio_console__init(kvm);
- if (r < 0) {
- pr_err("virtio_console__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = virtio_rng__init(kvm);
- if (r < 0) {
- pr_err("virtio_rng__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = virtio_bln__init(kvm);
- if (r < 0) {
- pr_err("virtio_rng__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = virtio_9p__init(kvm);
- if (r < 0) {
- pr_err("virtio_9p__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = virtio_net__init(kvm);
- if (r < 0) {
- pr_err("virtio_net__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = kbd__init(kvm);
- if (r < 0) {
- pr_err("kbd__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = pci_shmem__init(kvm);
- if (r < 0) {
- pr_err("pci_shmem__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = vnc__init(kvm);
- if (r < 0) {
- pr_err("vnc__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = sdl__init(kvm);
- if (r < 0) {
- pr_err("sdl__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = fb__init(kvm);
- if (r < 0) {
- pr_err("fb__init() failed with error %d\n", r);
- goto fail;
- }
-
- /*
- * Device init all done; firmware init must
- * come after this (it may set up device trees etc.)
- */
-
- r = kvm_timer__init(kvm);
- if (r < 0) {
- pr_err("kvm_timer__init() failed with error %d\n", r);
- goto fail;
- }
-
- r = thread_pool__init(kvm);
- if (r < 0) {
- pr_err("thread_pool__init() failed with error %d\n", r);
- goto fail;
- }
-
-fail:
- return r;
+ return init_list__init(kvm);
}
static int kvm_cmd_run_work(void)
@@ -818,97 +664,9 @@ static int kvm_cmd_run_work(void)
static void kvm_cmd_run_exit(int guest_ret)
{
- int r = 0;
-
compat__print_all_messages();
- r = kvm_cpu__exit(kvm);
- if (r < 0)
- pr_warning("kvm_cpu__exit() failed with error %d\n", r);
-
- r = symbol_exit(kvm);
- if (r < 0)
- pr_warning("symbol_exit() failed with error %d\n", r);
-
- r = irq__exit(kvm);
- if (r < 0)
- pr_warning("irq__exit() failed with error %d\n", r);
-
- r = kvm_timer__exit(kvm);
- if (r < 0)
- pr_warning("kvm_timer__exit() failed with error %d\n", r);
-
- r = fb__exit(kvm);
- if (r < 0)
- pr_warning("kvm_timer__exit() failed with error %d\n", r);
-
- r = virtio_net__exit(kvm);
- if (r < 0)
- pr_warning("virtio_net__exit() failed with error %d\n", r);
-
- r = virtio_scsi_exit(kvm);
- if (r < 0)
- pr_warning("virtio_scsi_exit() failed with error %d\n", r);
-
- r = virtio_blk__exit(kvm);
- if (r < 0)
- pr_warning("virtio_blk__exit() failed with error %d\n", r);
-
- r = virtio_rng__exit(kvm);
- if (r < 0)
- pr_warning("virtio_rng__exit() failed with error %d\n", r);
-
- r = virtio_bln__exit(kvm);
- if (r < 0)
- pr_warning("virtio_bln__exit() failed with error %d\n", r);
-
- r = virtio_console__exit(kvm);
- if (r < 0)
- pr_warning("virtio_console__exit() failed with error %d\n", r);
-
- r = pci_shmem__exit(kvm);
- if (r < 0)
- pr_warning("pci_shmem__exit() failed with error %d\n", r);
-
- r = disk_image__exit(kvm);
- if (r < 0)
- pr_warning("disk_image__exit() failed with error %d\n", r);
-
- r = serial8250__exit(kvm);
- if (r < 0)
- pr_warning("serial8250__exit() failed with error %d\n", r);
-
- r = rtc__exit(kvm);
- if (r < 0)
- pr_warning("rtc__exit() failed with error %d\n", r);
-
- r = kvm__arch_free_firmware(kvm);
- if (r < 0)
- pr_warning("kvm__arch_free_firmware() failed with error %d\n", r);
-
- r = ioport__exit(kvm);
- if (r < 0)
- pr_warning("ioport__exit() failed with error %d\n", r);
-
- r = ioeventfd__exit(kvm);
- if (r < 0)
- pr_warning("ioeventfd__exit() failed with error %d\n", r);
-
- r = pci__exit(kvm);
- if (r < 0)
- pr_warning("pci__exit() failed with error %d\n", r);
-
- r = term_exit(kvm);
- if (r < 0)
- pr_warning("pci__exit() failed with error %d\n", r);
-
- r = thread_pool__exit(kvm);
- if (r < 0)
- pr_warning("thread_pool__exit() failed with error %d\n", r);
-
- r = kvm__exit(kvm);
- if (r < 0)
- pr_warning("pci__exit() failed with error %d\n", r);
+ init_list__exit(kvm);
if (guest_ret == 0)
printf("\n # KVM session ended normally.\n");
@@ -345,8 +345,10 @@ int disk_image__init(struct kvm *kvm)
return 0;
}
+dev_base_init(disk_image__init);
int disk_image__exit(struct kvm *kvm)
{
return disk_image__close_all(kvm->disks, kvm->nr_disks);
}
+dev_base_exit(disk_image__exit);
@@ -1,4 +1,5 @@
#include "kvm/framebuffer.h"
+#include "kvm/kvm.h"
#include <linux/kernel.h>
#include <linux/list.h>
@@ -58,6 +59,7 @@ int fb__init(struct kvm *kvm)
return 0;
}
+firmware_init(fb__init);
int fb__exit(struct kvm *kvm)
{
@@ -75,3 +77,4 @@ int fb__exit(struct kvm *kvm)
return 0;
}
+firmware_exit(fb__exit);
@@ -352,3 +352,4 @@ int kbd__init(struct kvm *kvm)
return 0;
}
+dev_init(kbd__init);
@@ -396,8 +396,10 @@ int pci_shmem__init(struct kvm *kvm)
mem);
return 0;
}
+dev_init(pci_shmem__init);
int pci_shmem__exit(struct kvm *kvm)
{
return 0;
}
+dev_exit(pci_shmem__exit);
@@ -126,6 +126,7 @@ int rtc__init(struct kvm *kvm)
return r;
}
+dev_init(rtc__init);
int rtc__exit(struct kvm *kvm)
{
@@ -135,3 +136,4 @@ int rtc__exit(struct kvm *kvm)
return 0;
}
+dev_exit(rtc__exit);
@@ -432,6 +432,7 @@ cleanup:
return r;
}
+dev_init(serial8250__init);
int serial8250__exit(struct kvm *kvm)
{
@@ -448,3 +449,4 @@ int serial8250__exit(struct kvm *kvm)
return 0;
}
+dev_exit(serial8250__exit);
@@ -3,6 +3,8 @@
#include "kvm/kvm-arch.h"
#include "kvm/kvm-config.h"
+#include "kvm/util-init.h"
+#include "kvm/kvm.h"
#include <stdbool.h>
#include <linux/types.h>
@@ -93,6 +93,7 @@ cleanup:
return r;
}
+base_init(ioeventfd__init);
int ioeventfd__exit(struct kvm *kvm)
{
@@ -115,6 +116,7 @@ int ioeventfd__exit(struct kvm *kvm)
return 0;
}
+base_exit(ioeventfd__exit);
int ioeventfd__add_event(struct ioevent *ioevent, bool is_pio, bool poll_in_userspace)
{
@@ -188,9 +188,11 @@ int ioport__init(struct kvm *kvm)
return 0;
}
+dev_base_init(ioport__init);
int ioport__exit(struct kvm *kvm)
{
ioport__unregister_all();
return 0;
}
+dev_base_exit(ioport__exit);
@@ -212,6 +212,7 @@ fail_alloc:
free(kvm->cpus[i]);
return -ENOMEM;
}
+base_init(kvm_cpu__init);
int kvm_cpu__exit(struct kvm *kvm)
{
@@ -238,3 +239,4 @@ int kvm_cpu__exit(struct kvm *kvm)
return r;
}
+late_exit(kvm_cpu__exit);
@@ -477,6 +477,7 @@ err_epoll:
err:
return ret;
}
+base_init(kvm_ipc__init);
int kvm_ipc__exit(struct kvm *kvm)
{
@@ -494,3 +495,4 @@ int kvm_ipc__exit(struct kvm *kvm)
return ret;
}
+base_exit(kvm_ipc__exit);
@@ -140,6 +140,7 @@ int kvm__exit(struct kvm *kvm)
return 0;
}
+core_exit(kvm__exit);
/*
* Note: KVM_SET_USER_MEMORY_REGION assumes that we don't pass overlapping
@@ -274,6 +275,7 @@ err_free:
err:
return ret;
}
+core_init(kvm__init);
/* RFC 1952 */
#define GZIP_ID1 0x1f
@@ -375,6 +377,7 @@ int kvm_timer__init(struct kvm *kvm)
return 0;
}
+firmware_init(kvm_timer__init);
int kvm_timer__exit(struct kvm *kvm)
{
@@ -386,6 +389,7 @@ int kvm_timer__exit(struct kvm *kvm)
return 0;
}
+firmware_exit(kvm_timer__exit);
void kvm__dump_mem(struct kvm *kvm, unsigned long addr, unsigned long size)
{
@@ -203,6 +203,7 @@ int pci__init(struct kvm *kvm)
return 0;
}
+base_init(pci__init);
int pci__exit(struct kvm *kvm)
{
@@ -211,3 +212,4 @@ int pci__exit(struct kvm *kvm)
return 0;
}
+base_exit(pci__exit);
@@ -15,7 +15,7 @@ int symbol_init(struct kvm *kvm)
int ret = 0;
if (!kvm->vmlinux)
- return -EINVAL;
+ return 0;
bfd_init();
@@ -38,6 +38,7 @@ int symbol_init(struct kvm *kvm)
return ret;
}
+late_init(symbol_init);
static asymbol *lookup(asymbol **symbols, int nr_symbols, const char *symbol_name)
{
@@ -129,3 +130,4 @@ int symbol_exit(struct kvm *kvm)
return -EFAULT;
}
+late_exit(symbol_exit);
@@ -163,8 +163,10 @@ int term_init(struct kvm *kvm)
return 0;
}
+dev_init(term_init);
int term_exit(struct kvm *kvm)
{
return 0;
}
+dev_exit(term_exit);
@@ -309,8 +309,13 @@ int sdl__init(struct kvm *kvm)
return fb__attach(fb, &sdl_ops);
}
+dev_init(sdl__init);
int sdl__exit(struct kvm *kvm)
{
- return sdl__stop(NULL);
+ if (kvm->cfg.sdl)
+ return sdl__stop(NULL);
+
+ return 0;
}
+dev_exit(sdl__exit);
@@ -236,8 +236,13 @@ int vnc__init(struct kvm *kvm)
return fb__attach(fb, &vnc_ops);
}
+dev_init(vnc__init);
int vnc__exit(struct kvm *kvm)
{
- return vnc__stop(NULL);
+ if (kvm->cfg.vnc)
+ return vnc__stop(NULL);
+
+ return 0;
}
+dev_exit(vnc__exit);
@@ -1,5 +1,6 @@
#include "kvm/threadpool.h"
#include "kvm/mutex.h"
+#include "kvm/kvm.h"
#include <linux/kernel.h>
#include <linux/list.h>
@@ -131,6 +132,7 @@ int thread_pool__init(struct kvm *kvm)
return i;
}
+late_init(thread_pool__init);
int thread_pool__exit(struct kvm *kvm)
{
@@ -151,6 +153,7 @@ int thread_pool__exit(struct kvm *kvm)
return 0;
}
+late_exit(thread_pool__exit);
void thread_pool__do_job(struct thread_pool__job *job)
{
@@ -1404,6 +1404,7 @@ int virtio_9p__init(struct kvm *kvm)
return 0;
}
+virtio_dev_init(virtio_9p__init);
int virtio_9p__register(struct kvm *kvm, const char *root, const char *tag_name)
{
@@ -262,8 +262,10 @@ int virtio_bln__init(struct kvm *kvm)
return 0;
}
+virtio_dev_init(virtio_bln__init);
int virtio_bln__exit(struct kvm *kvm)
{
return 0;
}
+virtio_dev_exit(virtio_bln__exit);
@@ -299,6 +299,7 @@ int virtio_blk__init(struct kvm *kvm)
cleanup:
return virtio_blk__exit(kvm);
}
+virtio_dev_init(virtio_blk__init);
int virtio_blk__exit(struct kvm *kvm)
{
@@ -311,3 +312,4 @@ int virtio_blk__exit(struct kvm *kvm)
return 0;
}
+virtio_dev_exit(virtio_blk__exit);
@@ -194,8 +194,10 @@ int virtio_console__init(struct kvm *kvm)
return 0;
}
+virtio_dev_init(virtio_console__init);
int virtio_console__exit(struct kvm *kvm)
{
return 0;
}
+virtio_dev_exit(virtio_console__exit);
@@ -663,8 +663,10 @@ int virtio_net__init(struct kvm *kvm)
return 0;
}
+virtio_dev_init(virtio_net__init);
int virtio_net__exit(struct kvm *kvm)
{
return 0;
}
+virtio_dev_exit(virtio_net__exit);
@@ -176,6 +176,7 @@ cleanup:
return r;
}
+virtio_dev_init(virtio_rng__init);
int virtio_rng__exit(struct kvm *kvm)
{
@@ -189,3 +190,4 @@ int virtio_rng__exit(struct kvm *kvm)
return 0;
}
+virtio_dev_exit(virtio_rng__exit);
@@ -293,6 +293,7 @@ int virtio_scsi_init(struct kvm *kvm)
cleanup:
return virtio_scsi_exit(kvm);
}
+virtio_dev_init(virtio_scsi_init);
int virtio_scsi_exit(struct kvm *kvm)
{
@@ -305,3 +306,4 @@ int virtio_scsi_exit(struct kvm *kvm)
return 0;
}
+virtio_dev_exit(virtio_scsi_exit);
@@ -172,6 +172,7 @@ int irq__init(struct kvm *kvm)
return 0;
}
+dev_base_init(irq__init);
int irq__exit(struct kvm *kvm)
{
@@ -195,6 +196,7 @@ int irq__exit(struct kvm *kvm)
return 0;
}
+dev_base_exit(irq__exit);
int irq__add_msix_route(struct kvm *kvm, struct msi_msg *msg)
{
Switch to using init/exit calls instead of the repeating call blocks in builtin-run. Signed-off-by: Sasha Levin <levinsasha928@gmail.com> --- tools/kvm/Makefile | 1 + tools/kvm/builtin-run.c | 246 +------------------------------------------- tools/kvm/disk/core.c | 2 + tools/kvm/framebuffer.c | 3 + tools/kvm/hw/i8042.c | 1 + tools/kvm/hw/pci-shmem.c | 2 + tools/kvm/hw/rtc.c | 2 + tools/kvm/hw/serial.c | 2 + tools/kvm/include/kvm/kvm.h | 2 + tools/kvm/ioeventfd.c | 2 + tools/kvm/ioport.c | 2 + tools/kvm/kvm-cpu.c | 2 + tools/kvm/kvm-ipc.c | 2 + tools/kvm/kvm.c | 4 + tools/kvm/pci.c | 2 + tools/kvm/symbol.c | 4 +- tools/kvm/term.c | 2 + tools/kvm/ui/sdl.c | 7 +- tools/kvm/ui/vnc.c | 7 +- tools/kvm/util/threadpool.c | 3 + tools/kvm/virtio/9p.c | 1 + tools/kvm/virtio/balloon.c | 2 + tools/kvm/virtio/blk.c | 2 + tools/kvm/virtio/console.c | 2 + tools/kvm/virtio/net.c | 2 + tools/kvm/virtio/rng.c | 2 + tools/kvm/virtio/scsi.c | 2 + tools/kvm/x86/irq.c | 2 + 28 files changed, 66 insertions(+), 247 deletions(-)