This was ugly, and now we get rid of it.
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
#define KB_SHIFT (10)
#define GB_SHIFT (30)
-struct kvm *kvm;
__thread struct kvm_cpu *current_kvm_cpu;
static int kvm_run_wrapper;
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];
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;
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];
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;
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;
}
}
"%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) {
/*
* 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
if (!kvm->cfg.kernel_filename) {
kernel_usage_with_options();
- return -EINVAL;
+ return ERR_PTR(-EINVAL);
}
kvm->cfg.vmlinux_filename = find_vmlinux();
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=")) {
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;
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();
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;
}
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;
}
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;
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;
}
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;
}
while (term_readable(dev->id) &&
dev->rxcnt < FIFO_LEN) {
- c = term_getc(dev->id);
+ c = term_getc(kvm, dev->id);
if (c < 0)
break;
{
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;
for (j = 0; j <= i; j++) {
struct serial8250_device *dev = &devices[j];
- ioport__unregister(dev->iobase);
+ ioport__unregister(kvm, dev->iobase);
}
return r;
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;
}
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);
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
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);
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));
#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);
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();
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);
r = 0;
done:
- br_write_unlock();
+ br_write_unlock(kvm);
return r;
}
int ioport__init(struct kvm *kvm)
{
- ioport__setup_arch();
+ ioport__setup_arch(kvm);
return 0;
}
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;
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;
}
#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[];
}
}
-void kvm__pause(void)
+void kvm__pause(struct kvm *kvm)
{
int i, paused_vcpus = 0;
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)
return -errno;
}
}
- br_write_lock();
+ br_write_lock(kvm);
ret = mmio_insert(&mmio_tree, mmio);
- br_write_unlock();
+ br_write_unlock(kvm);
return ret;
}
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;
}
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;
{
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;
}
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;
}
#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 */
int term_fds[4][2];
-int term_getc(int term)
+int term_getc(struct kvm *kvm, int term)
{
unsigned char c;
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;
};
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)
}
}
-static int virtio_bln__collect_stats(void)
+static int virtio_bln__collect_stats(struct kvm *kvm)
{
u64 tmp;
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));
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);
}
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);
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);
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;
}
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)
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;
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;
}
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);
.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);
}