diff mbox

[33/33] kvm tools: remove global kvm object

Message ID 1346833927-15740-34-git-send-email-levinsasha928@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Sasha Levin Sept. 5, 2012, 8:32 a.m. UTC
This was ugly, and now we get rid of it.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
---
 tools/kvm/builtin-run.c        | 48 +++++++++++++++++++++++-------------------
 tools/kvm/hw/i8042.c           |  4 ++--
 tools/kvm/hw/pci-shmem.c       |  2 +-
 tools/kvm/hw/rtc.c             | 10 ++++-----
 tools/kvm/hw/serial.c          |  8 +++----
 tools/kvm/hw/vesa.c            |  2 +-
 tools/kvm/include/kvm/brlock.h | 16 +++++++-------
 tools/kvm/include/kvm/ioport.h |  7 +++---
 tools/kvm/include/kvm/kvm.h    |  4 ++--
 tools/kvm/include/kvm/term.h   |  4 ++--
 tools/kvm/ioport.c             | 16 +++++++-------
 tools/kvm/kvm-cpu.c            |  2 +-
 tools/kvm/kvm-ipc.c            |  4 ++--
 tools/kvm/kvm.c                |  6 ++----
 tools/kvm/mmio.c               | 10 ++++-----
 tools/kvm/pci.c                | 10 ++++-----
 tools/kvm/term.c               |  7 +++---
 tools/kvm/virtio/balloon.c     |  5 ++---
 tools/kvm/virtio/console.c     |  2 +-
 tools/kvm/virtio/net.c         | 12 +++++------
 tools/kvm/virtio/pci.c         |  6 +++---
 tools/kvm/x86/ioport.c         | 20 +++++++++---------
 22 files changed, 102 insertions(+), 103 deletions(-)
diff mbox

Patch

diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c
index 0ad49c1..85b9238 100644
--- a/tools/kvm/builtin-run.c
+++ b/tools/kvm/builtin-run.c
@@ -53,7 +53,6 @@ 
 #define KB_SHIFT		(10)
 #define GB_SHIFT		(30)
 
-struct kvm *kvm;
 __thread struct kvm_cpu *current_kvm_cpu;
 
 static int  kvm_run_wrapper;
@@ -339,11 +338,13 @@  static const char *find_vmlinux(void)
 
 void kvm_run_help(void)
 {
+	struct kvm *kvm = NULL;
+
 	BUILD_OPTIONS(options, &kvm->cfg, kvm);
 	usage_with_options(run_usage, options);
 }
 
-static int kvm_setup_guest_init(void)
+static int kvm_setup_guest_init(struct kvm *kvm)
 {
 	const char *rootfs = kvm->cfg.custom_rootfs_name;
 	char tmp[PATH_MAX];
@@ -367,7 +368,7 @@  static int kvm_setup_guest_init(void)
 	return 0;
 }
 
-static int kvm_run_set_sandbox(void)
+static int kvm_run_set_sandbox(struct kvm *kvm)
 {
 	const char *guestfs_name = kvm->cfg.custom_rootfs_name;
 	char path[PATH_MAX], script[PATH_MAX], *tmp;
@@ -439,7 +440,7 @@  static void resolve_program(const char *src, char *dst, size_t len)
 		strncpy(dst, src, len);
 }
 
-static void kvm_run_write_sandbox_cmd(const char **argv, int argc)
+static void kvm_run_write_sandbox_cmd(struct kvm *kvm, const char **argv, int argc)
 {
 	const char script_hdr[] = "#! /bin/bash\n\n";
 	char program[PATH_MAX];
@@ -474,15 +475,15 @@  static void kvm_run_write_sandbox_cmd(const char **argv, int argc)
 	close(fd);
 }
 
-static int kvm_cmd_run_init(int argc, const char **argv)
+static struct kvm *kvm_cmd_run_init(int argc, const char **argv)
 {
 	static char real_cmdline[2048], default_name[20];
 	unsigned int nr_online_cpus;
 	struct sigaction sa;
+	struct kvm *kvm = kvm__new();
 
-	kvm = kvm__new();
 	if (IS_ERR(kvm))
-		return PTR_ERR(kvm);
+		return kvm;
 
 	sa.sa_flags = SA_SIGINFO;
 	sa.sa_sigaction = handle_sigalrm;
@@ -502,7 +503,7 @@  static int kvm_cmd_run_init(int argc, const char **argv)
 			if (strcmp(argv[0], "--") == 0) {
 				if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
 					kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
-					kvm_run_write_sandbox_cmd(argv+1, argc-1);
+					kvm_run_write_sandbox_cmd(kvm, argv+1, argc-1);
 					break;
 				}
 			}
@@ -513,7 +514,7 @@  static int kvm_cmd_run_init(int argc, const char **argv)
 						"%s\n", argv[0]);
 				usage_with_options(run_usage, options);
 				free(kvm);
-				return -EINVAL;
+				return ERR_PTR(-EINVAL);
 			}
 			if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
 				/*
@@ -521,7 +522,7 @@  static int kvm_cmd_run_init(int argc, const char **argv)
 				 * sandbox command
 				 */
 				kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
-				kvm_run_write_sandbox_cmd(argv, argc);
+				kvm_run_write_sandbox_cmd(kvm, argv, argc);
 			} else {
 				/*
 				 * first unhandled parameter is treated as a kernel
@@ -542,7 +543,7 @@  static int kvm_cmd_run_init(int argc, const char **argv)
 
 	if (!kvm->cfg.kernel_filename) {
 		kernel_usage_with_options();
-		return -EINVAL;
+		return ERR_PTR(-EINVAL);
 	}
 
 	kvm->cfg.vmlinux_filename = find_vmlinux();
@@ -633,13 +634,13 @@  static int kvm_cmd_run_init(int argc, const char **argv)
 	if (kvm->cfg.using_rootfs) {
 		strcat(real_cmdline, " root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p");
 		if (kvm->cfg.custom_rootfs) {
-			kvm_run_set_sandbox();
+			kvm_run_set_sandbox(kvm);
 
 			strcat(real_cmdline, " init=/virt/init");
 
 			if (!kvm->cfg.no_dhcp)
 				strcat(real_cmdline, "  ip=dhcp");
-			if (kvm_setup_guest_init())
+			if (kvm_setup_guest_init(kvm))
 				die("Failed to setup init for guest.");
 		}
 	} else if (!strstr(real_cmdline, "root=")) {
@@ -651,10 +652,12 @@  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);
 
-	return init_list__init(kvm);
+	init_list__init(kvm);
+
+	return kvm;
 }
 
-static int kvm_cmd_run_work(void)
+static int kvm_cmd_run_work(struct kvm *kvm)
 {
 	int i;
 	void *ret = NULL;
@@ -668,7 +671,7 @@  static int kvm_cmd_run_work(void)
 	return pthread_join(kvm->cpus[0]->thread, &ret);
 }
 
-static void kvm_cmd_run_exit(int guest_ret)
+static void kvm_cmd_run_exit(struct kvm *kvm, int guest_ret)
 {
 	compat__print_all_messages();
 
@@ -680,14 +683,15 @@  static void kvm_cmd_run_exit(int guest_ret)
 
 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 {
-	int r, ret = -EFAULT;
+	int ret = -EFAULT;
+	struct kvm *kvm;
 
-	r = kvm_cmd_run_init(argc, argv);
-	if (r < 0)
-		return r;
+	kvm = kvm_cmd_run_init(argc, argv);
+	if (IS_ERR(kvm))
+		return PTR_ERR(kvm);
 
-	ret = kvm_cmd_run_work();
-	kvm_cmd_run_exit(ret);
+	ret = kvm_cmd_run_work(kvm);
+	kvm_cmd_run_exit(kvm, ret);
 
 	return ret;
 }
diff --git a/tools/kvm/hw/i8042.c b/tools/kvm/hw/i8042.c
index 5893d1d..9f8be6a 100644
--- a/tools/kvm/hw/i8042.c
+++ b/tools/kvm/hw/i8042.c
@@ -347,8 +347,8 @@  int kbd__init(struct kvm *kvm)
 
 	kbd_reset();
 	state.kvm = kvm;
-	ioport__register(I8042_DATA_REG, &kbd_ops, 2, NULL);
-	ioport__register(I8042_COMMAND_REG, &kbd_ops, 2, NULL);
+	ioport__register(kvm, I8042_DATA_REG, &kbd_ops, 2, NULL);
+	ioport__register(kvm, I8042_COMMAND_REG, &kbd_ops, 2, NULL);
 
 	return 0;
 }
diff --git a/tools/kvm/hw/pci-shmem.c b/tools/kvm/hw/pci-shmem.c
index 0eefa4e..4161335 100644
--- a/tools/kvm/hw/pci-shmem.c
+++ b/tools/kvm/hw/pci-shmem.c
@@ -362,7 +362,7 @@  int pci_shmem__init(struct kvm *kvm)
 	pci_shmem_pci_device.irq_line = line;
 
 	/* Register MMIO space for MSI-X */
-	r = ioport__register(IOPORT_EMPTY, &shmem_pci__io_ops, IOPORT_SIZE, NULL);
+	r = ioport__register(kvm, IOPORT_EMPTY, &shmem_pci__io_ops, IOPORT_SIZE, NULL);
 	if (r < 0)
 		return r;
 	ivshmem_registers = (u16)r;
diff --git a/tools/kvm/hw/rtc.c b/tools/kvm/hw/rtc.c
index dd6dca3..ad6dd65 100644
--- a/tools/kvm/hw/rtc.c
+++ b/tools/kvm/hw/rtc.c
@@ -114,13 +114,13 @@  int rtc__init(struct kvm *kvm)
 	int r = 0;
 
 	/* PORT 0070-007F - CMOS RAM/RTC (REAL TIME CLOCK) */
-	r = ioport__register(0x0070, &cmos_ram_index_ioport_ops, 1, NULL);
+	r = ioport__register(kvm, 0x0070, &cmos_ram_index_ioport_ops, 1, NULL);
 	if (r < 0)
 		return r;
 
-	r = ioport__register(0x0071, &cmos_ram_data_ioport_ops, 1, NULL);
+	r = ioport__register(kvm, 0x0071, &cmos_ram_data_ioport_ops, 1, NULL);
 	if (r < 0) {
-		ioport__unregister(0x0071);
+		ioport__unregister(kvm, 0x0071);
 		return r;
 	}
 
@@ -131,8 +131,8 @@  dev_init(rtc__init);
 int rtc__exit(struct kvm *kvm)
 {
 	/* PORT 0070-007F - CMOS RAM/RTC (REAL TIME CLOCK) */
-	ioport__unregister(0x0070);
-	ioport__unregister(0x0071);
+	ioport__unregister(kvm, 0x0070);
+	ioport__unregister(kvm, 0x0071);
 
 	return 0;
 }
diff --git a/tools/kvm/hw/serial.c b/tools/kvm/hw/serial.c
index 5348a92..a177a7f 100644
--- a/tools/kvm/hw/serial.c
+++ b/tools/kvm/hw/serial.c
@@ -195,7 +195,7 @@  static void serial8250__receive(struct kvm *kvm, struct serial8250_device *dev,
 	while (term_readable(dev->id) &&
 	       dev->rxcnt < FIFO_LEN) {
 
-		c = term_getc(dev->id);
+		c = term_getc(kvm, dev->id);
 
 		if (c < 0)
 			break;
@@ -403,7 +403,7 @@  static int serial8250__device_init(struct kvm *kvm, struct serial8250_device *de
 {
 	int r;
 
-	r = ioport__register(dev->iobase, &serial8250_ops, 8, NULL);
+	r = ioport__register(kvm, dev->iobase, &serial8250_ops, 8, NULL);
 	kvm__irq_line(kvm, dev->irq, 0);
 
 	return r;
@@ -427,7 +427,7 @@  cleanup:
 	for (j = 0; j <= i; j++) {
 		struct serial8250_device *dev = &devices[j];
 
-		ioport__unregister(dev->iobase);
+		ioport__unregister(kvm, dev->iobase);
 	}
 
 	return r;
@@ -442,7 +442,7 @@  int serial8250__exit(struct kvm *kvm)
 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
 		struct serial8250_device *dev = &devices[i];
 
-		r = ioport__unregister(dev->iobase);
+		r = ioport__unregister(kvm, dev->iobase);
 		if (r < 0)
 			return r;
 	}
diff --git a/tools/kvm/hw/vesa.c b/tools/kvm/hw/vesa.c
index 6f2d9f4..a211491 100644
--- a/tools/kvm/hw/vesa.c
+++ b/tools/kvm/hw/vesa.c
@@ -60,7 +60,7 @@  struct framebuffer *vesa__init(struct kvm *kvm)
 	if (r < 0)
 		return ERR_PTR(r);
 
-	r = ioport__register(IOPORT_EMPTY, &vesa_io_ops, IOPORT_SIZE, NULL);
+	r = ioport__register(kvm, IOPORT_EMPTY, &vesa_io_ops, IOPORT_SIZE, NULL);
 	if (r < 0)
 		return ERR_PTR(r);
 
diff --git a/tools/kvm/include/kvm/brlock.h b/tools/kvm/include/kvm/brlock.h
index bd1d882..29f72e0 100644
--- a/tools/kvm/include/kvm/brlock.h
+++ b/tools/kvm/include/kvm/brlock.h
@@ -23,19 +23,19 @@ 
 
 DECLARE_RWSEM(brlock_sem);
 
-#define br_read_lock()		down_read(&brlock_sem);
-#define br_read_unlock()	up_read(&brlock_sem);
+#define br_read_lock(kvm)	down_read(&brlock_sem);
+#define br_read_unlock(kvm)	up_read(&brlock_sem);
 
-#define br_write_lock()		down_write(&brlock_sem);
-#define br_write_unlock()	up_write(&brlock_sem);
+#define br_write_lock(kvm)	down_write(&brlock_sem);
+#define br_write_unlock(kvm)	up_write(&brlock_sem);
 
 #else
 
-#define br_read_lock()		barrier()
-#define br_read_unlock()	barrier()
+#define br_read_lock(kvm)	barrier()
+#define br_read_unlock(kvm)	barrier()
 
-#define br_write_lock()		kvm__pause()
-#define br_write_unlock()	kvm__continue()
+#define br_write_lock(kvm)	kvm__pause(kvm)
+#define br_write_unlock(kvm)	kvm__continue(kvm)
 #endif
 
 #endif
diff --git a/tools/kvm/include/kvm/ioport.h b/tools/kvm/include/kvm/ioport.h
index ced8cf5..6660acb 100644
--- a/tools/kvm/include/kvm/ioport.h
+++ b/tools/kvm/include/kvm/ioport.h
@@ -29,10 +29,11 @@  struct ioport_operations {
 	bool (*io_out)(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size);
 };
 
-void ioport__setup_arch(void);
+void ioport__setup_arch(struct kvm *kvm);
 
-int ioport__register(u16 port, struct ioport_operations *ops, int count, void *param);
-int ioport__unregister(u16 port);
+int ioport__register(struct kvm *kvm, u16 port, struct ioport_operations *ops,
+			int count, void *param);
+int ioport__unregister(struct kvm *kvm, u16 port);
 int ioport__init(struct kvm *kvm);
 int ioport__exit(struct kvm *kvm);
 
diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h
index f009695..b460656 100644
--- a/tools/kvm/include/kvm/kvm.h
+++ b/tools/kvm/include/kvm/kvm.h
@@ -81,8 +81,8 @@  int kvm__register_mmio(struct kvm *kvm, u64 phys_addr, u64 phys_addr_len, bool c
 			void (*mmio_fn)(u64 addr, u8 *data, u32 len, u8 is_write, void *ptr),
 			void *ptr);
 bool kvm__deregister_mmio(struct kvm *kvm, u64 phys_addr);
-void kvm__pause(void);
-void kvm__continue(void);
+void kvm__pause(struct kvm *kvm);
+void kvm__continue(struct kvm *kvm);
 void kvm__notify_paused(void);
 int kvm__get_sock_by_instance(const char *name);
 int kvm__enumerate_instances(int (*callback)(const char *name, int pid));
diff --git a/tools/kvm/include/kvm/term.h b/tools/kvm/include/kvm/term.h
index 493ce39..5f63457 100644
--- a/tools/kvm/include/kvm/term.h
+++ b/tools/kvm/include/kvm/term.h
@@ -11,9 +11,9 @@ 
 #define CONSOLE_HV	3
 
 int term_putc_iov(struct iovec *iov, int iovcnt, int term);
-int term_getc_iov(struct iovec *iov, int iovcnt, int term);
+int term_getc_iov(struct kvm *kvm, struct iovec *iov, int iovcnt, int term);
 int term_putc(char *addr, int cnt, int term);
-int term_getc(int term);
+int term_getc(struct kvm *kvm, int term);
 
 bool term_readable(int term);
 void term_set_tty(int term);
diff --git a/tools/kvm/ioport.c b/tools/kvm/ioport.c
index 8d6e382..a4f1582 100644
--- a/tools/kvm/ioport.c
+++ b/tools/kvm/ioport.c
@@ -55,12 +55,12 @@  static void ioport_remove(struct rb_root *root, struct ioport *data)
 	rb_int_erase(root, &data->node);
 }
 
-int ioport__register(u16 port, struct ioport_operations *ops, int count, void *param)
+int ioport__register(struct kvm *kvm, u16 port, struct ioport_operations *ops, int count, void *param)
 {
 	struct ioport *entry;
 	int r;
 
-	br_write_lock();
+	br_write_lock(kvm);
 	if (port == IOPORT_EMPTY)
 		port = ioport__find_free_port();
 
@@ -83,20 +83,20 @@  int ioport__register(u16 port, struct ioport_operations *ops, int count, void *p
 	r = ioport_insert(&ioport_tree, entry);
 	if (r < 0) {
 		free(entry);
-		br_write_unlock();
+		br_write_unlock(kvm);
 		return r;
 	}
-	br_write_unlock();
+	br_write_unlock(kvm);
 
 	return port;
 }
 
-int ioport__unregister(u16 port)
+int ioport__unregister(struct kvm *kvm, u16 port)
 {
 	struct ioport *entry;
 	int r;
 
-	br_write_lock();
+	br_write_lock(kvm);
 
 	r = -ENOENT;
 	entry = ioport_search(&ioport_tree, port);
@@ -110,7 +110,7 @@  int ioport__unregister(u16 port)
 	r = 0;
 
 done:
-	br_write_unlock();
+	br_write_unlock(kvm);
 
 	return r;
 }
@@ -184,7 +184,7 @@  error:
 
 int ioport__init(struct kvm *kvm)
 {
-	ioport__setup_arch();
+	ioport__setup_arch(kvm);
 
 	return 0;
 }
diff --git a/tools/kvm/kvm-cpu.c b/tools/kvm/kvm-cpu.c
index 86d3b04..be05c49 100644
--- a/tools/kvm/kvm-cpu.c
+++ b/tools/kvm/kvm-cpu.c
@@ -41,7 +41,7 @@  static void kvm_cpu_signal_handler(int signum)
 	if (signum == SIGKVMEXIT) {
 		if (current_kvm_cpu && current_kvm_cpu->is_running) {
 			current_kvm_cpu->is_running = false;
-			kvm__continue();
+			kvm__continue(current_kvm_cpu->kvm);
 		}
 	} else if (signum == SIGKVMPAUSE) {
 		current_kvm_cpu->paused = 1;
diff --git a/tools/kvm/kvm-ipc.c b/tools/kvm/kvm-ipc.c
index 6b47270..d23edd2 100644
--- a/tools/kvm/kvm-ipc.c
+++ b/tools/kvm/kvm-ipc.c
@@ -321,11 +321,11 @@  static void handle_pause(struct kvm *kvm, int fd, u32 type, u32 len, u8 *msg)
 
 	if (type == KVM_IPC_RESUME && is_paused) {
 		kvm->vm_state = KVM_VMSTATE_RUNNING;
-		kvm__continue();
+		kvm__continue(kvm);
 	} else if (type == KVM_IPC_PAUSE && !is_paused) {
 		kvm->vm_state = KVM_VMSTATE_PAUSED;
 		ioctl(kvm->vm_fd, KVM_KVMCLOCK_CTRL);
-		kvm__pause();
+		kvm__pause(kvm);
 	} else {
 		return;
 	}
diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c
index d107931..b283171 100644
--- a/tools/kvm/kvm.c
+++ b/tools/kvm/kvm.c
@@ -55,8 +55,6 @@  const char *kvm_exit_reasons[] = {
 #endif
 };
 
-extern struct kvm *kvm;
-extern struct kvm_cpu **kvm_cpus;
 static int pause_event;
 static DEFINE_MUTEX(pause_lock);
 extern struct kvm_ext kvm_req_ext[];
@@ -413,7 +411,7 @@  void kvm__dump_mem(struct kvm *kvm, unsigned long addr, unsigned long size)
 	}
 }
 
-void kvm__pause(void)
+void kvm__pause(struct kvm *kvm)
 {
 	int i, paused_vcpus = 0;
 
@@ -439,7 +437,7 @@  void kvm__pause(void)
 	close(pause_event);
 }
 
-void kvm__continue(void)
+void kvm__continue(struct kvm *kvm)
 {
 	/* Check if the guest is running */
 	if (!kvm->cpus[0] || kvm->cpus[0]->thread == 0)
diff --git a/tools/kvm/mmio.c b/tools/kvm/mmio.c
index 38ce117..5d65d28 100644
--- a/tools/kvm/mmio.c
+++ b/tools/kvm/mmio.c
@@ -87,9 +87,9 @@  int kvm__register_mmio(struct kvm *kvm, u64 phys_addr, u64 phys_addr_len, bool c
 			return -errno;
 		}
 	}
-	br_write_lock();
+	br_write_lock(kvm);
 	ret = mmio_insert(&mmio_tree, mmio);
-	br_write_unlock();
+	br_write_unlock(kvm);
 
 	return ret;
 }
@@ -99,10 +99,10 @@  bool kvm__deregister_mmio(struct kvm *kvm, u64 phys_addr)
 	struct mmio_mapping *mmio;
 	struct kvm_coalesced_mmio_zone zone;
 
-	br_write_lock();
+	br_write_lock(kvm);
 	mmio = mmio_search_single(&mmio_tree, phys_addr);
 	if (mmio == NULL) {
-		br_write_unlock();
+		br_write_unlock(kvm);
 		return false;
 	}
 
@@ -113,7 +113,7 @@  bool kvm__deregister_mmio(struct kvm *kvm, u64 phys_addr)
 	ioctl(kvm->vm_fd, KVM_UNREGISTER_COALESCED_MMIO, &zone);
 
 	rb_int_erase(&mmio_tree, &mmio->node);
-	br_write_unlock();
+	br_write_unlock(kvm);
 
 	free(mmio);
 	return true;
diff --git a/tools/kvm/pci.c b/tools/kvm/pci.c
index fc4ad39..a28b5e2 100644
--- a/tools/kvm/pci.c
+++ b/tools/kvm/pci.c
@@ -191,13 +191,13 @@  int pci__init(struct kvm *kvm)
 {
 	int r;
 
-	r = ioport__register(PCI_CONFIG_DATA + 0, &pci_config_data_ops, 4, NULL);
+	r = ioport__register(kvm, PCI_CONFIG_DATA + 0, &pci_config_data_ops, 4, NULL);
 	if (r < 0)
 		return r;
 
-	r = ioport__register(PCI_CONFIG_ADDRESS + 0, &pci_config_address_ops, 4, NULL);
+	r = ioport__register(kvm, PCI_CONFIG_ADDRESS + 0, &pci_config_address_ops, 4, NULL);
 	if (r < 0) {
-		ioport__unregister(PCI_CONFIG_DATA);
+		ioport__unregister(kvm, PCI_CONFIG_DATA);
 		return r;
 	}
 
@@ -207,8 +207,8 @@  base_init(pci__init);
 
 int pci__exit(struct kvm *kvm)
 {
-	ioport__unregister(PCI_CONFIG_DATA);
-	ioport__unregister(PCI_CONFIG_ADDRESS);
+	ioport__unregister(kvm, PCI_CONFIG_DATA);
+	ioport__unregister(kvm, PCI_CONFIG_ADDRESS);
 
 	return 0;
 }
diff --git a/tools/kvm/term.c b/tools/kvm/term.c
index d47a75e..4413450 100644
--- a/tools/kvm/term.c
+++ b/tools/kvm/term.c
@@ -17,7 +17,6 @@ 
 #define TERM_FD_IN      0
 #define TERM_FD_OUT     1
 
-extern struct kvm *kvm;
 static struct termios	orig_term;
 
 int term_escape_char	= 0x01; /* ctrl-a is used for escape */
@@ -25,7 +24,7 @@  bool term_got_escape	= false;
 
 int term_fds[4][2];
 
-int term_getc(int term)
+int term_getc(struct kvm *kvm, int term)
 {
 	unsigned char c;
 
@@ -61,11 +60,11 @@  int term_putc(char *addr, int cnt, int term)
 	return cnt;
 }
 
-int term_getc_iov(struct iovec *iov, int iovcnt, int term)
+int term_getc_iov(struct kvm *kvm, struct iovec *iov, int iovcnt, int term)
 {
 	int c;
 
-	c = term_getc(term);
+	c = term_getc(kvm, term);
 
 	if (c < 0)
 		return 0;
diff --git a/tools/kvm/virtio/balloon.c b/tools/kvm/virtio/balloon.c
index d6bde32..e08f628 100644
--- a/tools/kvm/virtio/balloon.c
+++ b/tools/kvm/virtio/balloon.c
@@ -48,7 +48,6 @@  struct bln_dev {
 };
 
 static struct bln_dev bdev;
-extern struct kvm *kvm;
 static int compat_id = -1;
 
 static bool virtio_bln_do_io_request(struct kvm *kvm, struct bln_dev *bdev, struct virt_queue *queue)
@@ -125,7 +124,7 @@  static void virtio_bln_do_io(struct kvm *kvm, void *param)
 	}
 }
 
-static int virtio_bln__collect_stats(void)
+static int virtio_bln__collect_stats(struct kvm *kvm)
 {
 	u64 tmp;
 
@@ -146,7 +145,7 @@  static void virtio_bln__print_stats(struct kvm *kvm, int fd, u32 type, u32 len,
 	if (WARN_ON(type != KVM_IPC_STAT || len))
 		return;
 
-	if (virtio_bln__collect_stats() < 0)
+	if (virtio_bln__collect_stats(kvm) < 0)
 		return;
 
 	r = write(fd, bdev.stats, sizeof(bdev.stats));
diff --git a/tools/kvm/virtio/console.c b/tools/kvm/virtio/console.c
index b4c0463..88b1106 100644
--- a/tools/kvm/virtio/console.c
+++ b/tools/kvm/virtio/console.c
@@ -71,7 +71,7 @@  static void virtio_console__inject_interrupt_callback(struct kvm *kvm, void *par
 
 	if (term_readable(0) && virt_queue__available(vq)) {
 		head = virt_queue__get_iov(vq, iov, &out, &in, kvm);
-		len = term_getc_iov(iov, in, 0);
+		len = term_getc_iov(kvm, iov, in, 0);
 		virt_queue__set_used_elem(vq, head, len);
 		cdev.vdev.ops->signal_vq(kvm, &cdev.vdev, vq - cdev.vqs);
 	}
diff --git a/tools/kvm/virtio/net.c b/tools/kvm/virtio/net.c
index e6da7f8..d6d344a 100644
--- a/tools/kvm/virtio/net.c
+++ b/tools/kvm/virtio/net.c
@@ -33,8 +33,6 @@ 
 
 struct net_dev;
 
-extern struct kvm *kvm;
-
 struct net_dev_operations {
 	int (*rx)(struct iovec *iov, u16 in, struct net_dev *ndev);
 	int (*tx)(struct iovec *iov, u16 in, struct net_dev *ndev);
@@ -495,8 +493,8 @@  static inline void str_to_mac(const char *str, char *mac)
 	sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
 		mac, mac+1, mac+2, mac+3, mac+4, mac+5);
 }
-static int set_net_param(struct virtio_net_params *p, const char *param,
-				const char *val)
+static int set_net_param(struct kvm *kvm, struct virtio_net_params *p,
+			const char *param, const char *val)
 {
 	if (strcmp(param, "guest_mac") == 0) {
 		str_to_mac(val, p->guest_mac);
@@ -561,7 +559,7 @@  int netdev_parser(const struct option *opt, const char *arg, int unset)
 		if (on_cmd) {
 			cmd = cur;
 		} else {
-			if (set_net_param(&p, cmd, cur) < 0)
+			if (set_net_param(kvm, &p, cmd, cur) < 0)
 				goto done;
 		}
 		on_cmd = !on_cmd;
@@ -619,10 +617,10 @@  static int virtio_net__init_one(struct virtio_net_params *params)
 	}
 
 	if (params->trans && strcmp(params->trans, "mmio") == 0)
-		virtio_init(kvm, ndev, &ndev->vdev, &net_dev_virtio_ops,
+		virtio_init(params->kvm, ndev, &ndev->vdev, &net_dev_virtio_ops,
 			    VIRTIO_MMIO, PCI_DEVICE_ID_VIRTIO_NET, VIRTIO_ID_NET, PCI_CLASS_NET);
 	else
-		virtio_init(kvm, ndev, &ndev->vdev, &net_dev_virtio_ops,
+		virtio_init(params->kvm, ndev, &ndev->vdev, &net_dev_virtio_ops,
 			    VIRTIO_PCI, PCI_DEVICE_ID_VIRTIO_NET, VIRTIO_ID_NET, PCI_CLASS_NET);
 
 	if (params->vhost)
diff --git a/tools/kvm/virtio/pci.c b/tools/kvm/virtio/pci.c
index 81f95ae..b6ac571 100644
--- a/tools/kvm/virtio/pci.c
+++ b/tools/kvm/virtio/pci.c
@@ -307,7 +307,7 @@  int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 	vpci->dev = dev;
 	vpci->msix_io_block = pci_get_io_space_block(PCI_IO_SIZE * 2);
 
-	r = ioport__register(IOPORT_EMPTY, &virtio_pci__io_ops, IOPORT_SIZE, vdev);
+	r = ioport__register(kvm, IOPORT_EMPTY, &virtio_pci__io_ops, IOPORT_SIZE, vdev);
 	if (r < 0)
 		return r;
 
@@ -379,7 +379,7 @@  int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 free_mmio:
 	kvm__deregister_mmio(kvm, vpci->msix_io_block);
 free_ioport:
-	ioport__unregister(vpci->base_addr);
+	ioport__unregister(kvm, vpci->base_addr);
 	return r;
 }
 
@@ -389,7 +389,7 @@  int virtio_pci__exit(struct kvm *kvm, struct virtio_device *vdev)
 	int i;
 
 	kvm__deregister_mmio(kvm, vpci->msix_io_block);
-	ioport__unregister(vpci->base_addr);
+	ioport__unregister(kvm, vpci->base_addr);
 
 	for (i = 0; i < VIRTIO_PCI_MAX_VQ; i++)
 		ioeventfd__del_event(vpci->base_addr + VIRTIO_PCI_QUEUE_NOTIFY, i);
diff --git a/tools/kvm/x86/ioport.c b/tools/kvm/x86/ioport.c
index 86302e6..4993f9d 100644
--- a/tools/kvm/x86/ioport.c
+++ b/tools/kvm/x86/ioport.c
@@ -46,32 +46,32 @@  static struct ioport_operations dummy_write_only_ioport_ops = {
 	.io_out		= dummy_io_out,
 };
 
-void ioport__setup_arch(void)
+void ioport__setup_arch(struct kvm *kvm)
 {
 	/* Legacy ioport setup */
 
 	/* 0x0020 - 0x003F - 8259A PIC 1 */
-	ioport__register(0x0020, &dummy_read_write_ioport_ops, 2, NULL);
+	ioport__register(kvm, 0x0020, &dummy_read_write_ioport_ops, 2, NULL);
 
 	/* PORT 0040-005F - PIT - PROGRAMMABLE INTERVAL TIMER (8253, 8254) */
-	ioport__register(0x0040, &dummy_read_write_ioport_ops, 4, NULL);
+	ioport__register(kvm, 0x0040, &dummy_read_write_ioport_ops, 4, NULL);
 
 	/* 0x00A0 - 0x00AF - 8259A PIC 2 */
-	ioport__register(0x00A0, &dummy_read_write_ioport_ops, 2, NULL);
+	ioport__register(kvm, 0x00A0, &dummy_read_write_ioport_ops, 2, NULL);
 
 	/* PORT 00E0-00EF are 'motherboard specific' so we use them for our
 	   internal debugging purposes.  */
-	ioport__register(IOPORT_DBG, &debug_ops, 1, NULL);
+	ioport__register(kvm, IOPORT_DBG, &debug_ops, 1, NULL);
 
 	/* PORT 00ED - DUMMY PORT FOR DELAY??? */
-	ioport__register(0x00ED, &dummy_write_only_ioport_ops, 1, NULL);
+	ioport__register(kvm, 0x00ED, &dummy_write_only_ioport_ops, 1, NULL);
 
 	/* 0x00F0 - 0x00FF - Math co-processor */
-	ioport__register(0x00F0, &dummy_write_only_ioport_ops, 2, NULL);
+	ioport__register(kvm, 0x00F0, &dummy_write_only_ioport_ops, 2, NULL);
 
 	/* PORT 03D4-03D5 - COLOR VIDEO - CRT CONTROL REGISTERS */
-	ioport__register(0x03D4, &dummy_read_write_ioport_ops, 1, NULL);
-	ioport__register(0x03D5, &dummy_write_only_ioport_ops, 1, NULL);
+	ioport__register(kvm, 0x03D4, &dummy_read_write_ioport_ops, 1, NULL);
+	ioport__register(kvm, 0x03D5, &dummy_write_only_ioport_ops, 1, NULL);
 
-	ioport__register(0x402, &seabios_debug_ops, 1, NULL);
+	ioport__register(kvm, 0x402, &seabios_debug_ops, 1, NULL);
 }