Commit bccf2150 authored by Avi Kivity's avatar Avi Kivity

KVM: Per-vcpu inodes

Allocate a distinct inode for every vcpu in a VM.  This has the following
benefits:

 - the filp cachelines are no longer bounced when f_count is incremented on
   every ioctl()
 - the API and internal code are distinctly clearer; for example, on the
   KVM_GET_REGS ioctl, there is no need to copy the vcpu number from
   userspace and then copy the registers back; the vcpu identity is derived
   from the fd used to make the call

Right now the performance benefits are completely theoretical since (a) we
don't support more than one vcpu per VM and (b) virtualization hardware
inefficiencies completely everwhelm any cacheline bouncing effects.  But
both of these will change, and we need to prepare the API today.
Signed-off-by: default avatarAvi Kivity <avi@qumranet.com>
parent c5ea7660
...@@ -309,6 +309,7 @@ struct kvm { ...@@ -309,6 +309,7 @@ struct kvm {
int busy; int busy;
unsigned long rmap_overflow; unsigned long rmap_overflow;
struct list_head vm_list; struct list_head vm_list;
struct file *filp;
}; };
struct kvm_stat { struct kvm_stat {
...@@ -343,7 +344,7 @@ struct kvm_arch_ops { ...@@ -343,7 +344,7 @@ struct kvm_arch_ops {
int (*vcpu_create)(struct kvm_vcpu *vcpu); int (*vcpu_create)(struct kvm_vcpu *vcpu);
void (*vcpu_free)(struct kvm_vcpu *vcpu); void (*vcpu_free)(struct kvm_vcpu *vcpu);
struct kvm_vcpu *(*vcpu_load)(struct kvm_vcpu *vcpu); void (*vcpu_load)(struct kvm_vcpu *vcpu);
void (*vcpu_put)(struct kvm_vcpu *vcpu); void (*vcpu_put)(struct kvm_vcpu *vcpu);
void (*vcpu_decache)(struct kvm_vcpu *vcpu); void (*vcpu_decache)(struct kvm_vcpu *vcpu);
......
...@@ -96,6 +96,9 @@ struct segment_descriptor_64 { ...@@ -96,6 +96,9 @@ struct segment_descriptor_64 {
#endif #endif
static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
unsigned long arg);
static struct inode *kvmfs_inode(struct file_operations *fops) static struct inode *kvmfs_inode(struct file_operations *fops)
{ {
int error = -ENOMEM; int error = -ENOMEM;
...@@ -246,24 +249,30 @@ int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size, ...@@ -246,24 +249,30 @@ int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
} }
EXPORT_SYMBOL_GPL(kvm_write_guest); EXPORT_SYMBOL_GPL(kvm_write_guest);
static int vcpu_slot(struct kvm_vcpu *vcpu) /*
* Switches to specified vcpu, until a matching vcpu_put()
*/
static void vcpu_load(struct kvm_vcpu *vcpu)
{ {
return vcpu - vcpu->kvm->vcpus; mutex_lock(&vcpu->mutex);
kvm_arch_ops->vcpu_load(vcpu);
} }
/* /*
* Switches to specified vcpu, until a matching vcpu_put() * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
* if the slot is not populated.
*/ */
static struct kvm_vcpu *vcpu_load(struct kvm *kvm, int vcpu_slot) static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot)
{ {
struct kvm_vcpu *vcpu = &kvm->vcpus[vcpu_slot]; struct kvm_vcpu *vcpu = &kvm->vcpus[slot];
mutex_lock(&vcpu->mutex); mutex_lock(&vcpu->mutex);
if (unlikely(!vcpu->vmcs)) { if (!vcpu->vmcs) {
mutex_unlock(&vcpu->mutex); mutex_unlock(&vcpu->mutex);
return NULL; return NULL;
} }
return kvm_arch_ops->vcpu_load(vcpu); kvm_arch_ops->vcpu_load(vcpu);
return vcpu;
} }
static void vcpu_put(struct kvm_vcpu *vcpu) static void vcpu_put(struct kvm_vcpu *vcpu)
...@@ -336,9 +345,10 @@ static void kvm_free_physmem(struct kvm *kvm) ...@@ -336,9 +345,10 @@ static void kvm_free_physmem(struct kvm *kvm)
static void kvm_free_vcpu(struct kvm_vcpu *vcpu) static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
{ {
if (!vcpu_load(vcpu->kvm, vcpu_slot(vcpu))) if (!vcpu->vmcs)
return; return;
vcpu_load(vcpu);
kvm_mmu_destroy(vcpu); kvm_mmu_destroy(vcpu);
vcpu_put(vcpu); vcpu_put(vcpu);
kvm_arch_ops->vcpu_free(vcpu); kvm_arch_ops->vcpu_free(vcpu);
...@@ -725,7 +735,7 @@ static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, ...@@ -725,7 +735,7 @@ static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
for (i = 0; i < KVM_MAX_VCPUS; ++i) { for (i = 0; i < KVM_MAX_VCPUS; ++i) {
struct kvm_vcpu *vcpu; struct kvm_vcpu *vcpu;
vcpu = vcpu_load(kvm, i); vcpu = vcpu_load_slot(kvm, i);
if (!vcpu) if (!vcpu)
continue; continue;
kvm_mmu_reset_context(vcpu); kvm_mmu_reset_context(vcpu);
...@@ -791,8 +801,9 @@ static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, ...@@ -791,8 +801,9 @@ static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
if (any) { if (any) {
cleared = 0; cleared = 0;
for (i = 0; i < KVM_MAX_VCPUS; ++i) { for (i = 0; i < KVM_MAX_VCPUS; ++i) {
struct kvm_vcpu *vcpu = vcpu_load(kvm, i); struct kvm_vcpu *vcpu;
vcpu = vcpu_load_slot(kvm, i);
if (!vcpu) if (!vcpu)
continue; continue;
if (!cleared) { if (!cleared) {
...@@ -1461,8 +1472,7 @@ void kvm_resched(struct kvm_vcpu *vcpu) ...@@ -1461,8 +1472,7 @@ void kvm_resched(struct kvm_vcpu *vcpu)
{ {
vcpu_put(vcpu); vcpu_put(vcpu);
cond_resched(); cond_resched();
/* Cannot fail - no vcpu unplug yet. */ vcpu_load(vcpu);
vcpu_load(vcpu->kvm, vcpu_slot(vcpu));
} }
EXPORT_SYMBOL_GPL(kvm_resched); EXPORT_SYMBOL_GPL(kvm_resched);
...@@ -1484,17 +1494,11 @@ void save_msrs(struct vmx_msr_entry *e, int n) ...@@ -1484,17 +1494,11 @@ void save_msrs(struct vmx_msr_entry *e, int n)
} }
EXPORT_SYMBOL_GPL(save_msrs); EXPORT_SYMBOL_GPL(save_msrs);
static int kvm_vm_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run) static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
{ {
struct kvm_vcpu *vcpu;
int r; int r;
if (!valid_vcpu(kvm_run->vcpu)) vcpu_load(vcpu);
return -EINVAL;
vcpu = vcpu_load(kvm, kvm_run->vcpu);
if (!vcpu)
return -ENOENT;
/* re-sync apic's tpr */ /* re-sync apic's tpr */
vcpu->cr8 = kvm_run->cr8; vcpu->cr8 = kvm_run->cr8;
...@@ -1517,16 +1521,10 @@ static int kvm_vm_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run) ...@@ -1517,16 +1521,10 @@ static int kvm_vm_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run)
return r; return r;
} }
static int kvm_vm_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs) static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
struct kvm_regs *regs)
{ {
struct kvm_vcpu *vcpu; vcpu_load(vcpu);
if (!valid_vcpu(regs->vcpu))
return -EINVAL;
vcpu = vcpu_load(kvm, regs->vcpu);
if (!vcpu)
return -ENOENT;
kvm_arch_ops->cache_regs(vcpu); kvm_arch_ops->cache_regs(vcpu);
...@@ -1563,16 +1561,10 @@ static int kvm_vm_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs) ...@@ -1563,16 +1561,10 @@ static int kvm_vm_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs)
return 0; return 0;
} }
static int kvm_vm_ioctl_set_regs(struct kvm *kvm, struct kvm_regs *regs) static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
struct kvm_regs *regs)
{ {
struct kvm_vcpu *vcpu; vcpu_load(vcpu);
if (!valid_vcpu(regs->vcpu))
return -EINVAL;
vcpu = vcpu_load(kvm, regs->vcpu);
if (!vcpu)
return -ENOENT;
vcpu->regs[VCPU_REGS_RAX] = regs->rax; vcpu->regs[VCPU_REGS_RAX] = regs->rax;
vcpu->regs[VCPU_REGS_RBX] = regs->rbx; vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
...@@ -1609,16 +1601,12 @@ static void get_segment(struct kvm_vcpu *vcpu, ...@@ -1609,16 +1601,12 @@ static void get_segment(struct kvm_vcpu *vcpu,
return kvm_arch_ops->get_segment(vcpu, var, seg); return kvm_arch_ops->get_segment(vcpu, var, seg);
} }
static int kvm_vm_ioctl_get_sregs(struct kvm *kvm, struct kvm_sregs *sregs) static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
struct kvm_sregs *sregs)
{ {
struct kvm_vcpu *vcpu;
struct descriptor_table dt; struct descriptor_table dt;
if (!valid_vcpu(sregs->vcpu)) vcpu_load(vcpu);
return -EINVAL;
vcpu = vcpu_load(kvm, sregs->vcpu);
if (!vcpu)
return -ENOENT;
get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
...@@ -1660,18 +1648,14 @@ static void set_segment(struct kvm_vcpu *vcpu, ...@@ -1660,18 +1648,14 @@ static void set_segment(struct kvm_vcpu *vcpu,
return kvm_arch_ops->set_segment(vcpu, var, seg); return kvm_arch_ops->set_segment(vcpu, var, seg);
} }
static int kvm_vm_ioctl_set_sregs(struct kvm *kvm, struct kvm_sregs *sregs) static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
struct kvm_sregs *sregs)
{ {
struct kvm_vcpu *vcpu;
int mmu_reset_needed = 0; int mmu_reset_needed = 0;
int i; int i;
struct descriptor_table dt; struct descriptor_table dt;
if (!valid_vcpu(sregs->vcpu)) vcpu_load(vcpu);
return -EINVAL;
vcpu = vcpu_load(kvm, sregs->vcpu);
if (!vcpu)
return -ENOENT;
set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
...@@ -1777,20 +1761,14 @@ static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) ...@@ -1777,20 +1761,14 @@ static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
* *
* @return number of msrs set successfully. * @return number of msrs set successfully.
*/ */
static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs, static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
struct kvm_msr_entry *entries, struct kvm_msr_entry *entries,
int (*do_msr)(struct kvm_vcpu *vcpu, int (*do_msr)(struct kvm_vcpu *vcpu,
unsigned index, u64 *data)) unsigned index, u64 *data))
{ {
struct kvm_vcpu *vcpu;
int i; int i;
if (!valid_vcpu(msrs->vcpu)) vcpu_load(vcpu);
return -EINVAL;
vcpu = vcpu_load(kvm, msrs->vcpu);
if (!vcpu)
return -ENOENT;
for (i = 0; i < msrs->nmsrs; ++i) for (i = 0; i < msrs->nmsrs; ++i)
if (do_msr(vcpu, entries[i].index, &entries[i].data)) if (do_msr(vcpu, entries[i].index, &entries[i].data))
...@@ -1806,7 +1784,7 @@ static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs, ...@@ -1806,7 +1784,7 @@ static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs,
* *
* @return number of msrs set successfully. * @return number of msrs set successfully.
*/ */
static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs, static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
int (*do_msr)(struct kvm_vcpu *vcpu, int (*do_msr)(struct kvm_vcpu *vcpu,
unsigned index, u64 *data), unsigned index, u64 *data),
int writeback) int writeback)
...@@ -1834,7 +1812,7 @@ static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs, ...@@ -1834,7 +1812,7 @@ static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs,
if (copy_from_user(entries, user_msrs->entries, size)) if (copy_from_user(entries, user_msrs->entries, size))
goto out_free; goto out_free;
r = n = __msr_io(kvm, &msrs, entries, do_msr); r = n = __msr_io(vcpu, &msrs, entries, do_msr);
if (r < 0) if (r < 0)
goto out_free; goto out_free;
...@@ -1853,38 +1831,31 @@ static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs, ...@@ -1853,38 +1831,31 @@ static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs,
/* /*
* Translate a guest virtual address to a guest physical address. * Translate a guest virtual address to a guest physical address.
*/ */
static int kvm_vm_ioctl_translate(struct kvm *kvm, struct kvm_translation *tr) static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
struct kvm_translation *tr)
{ {
unsigned long vaddr = tr->linear_address; unsigned long vaddr = tr->linear_address;
struct kvm_vcpu *vcpu;
gpa_t gpa; gpa_t gpa;
vcpu = vcpu_load(kvm, tr->vcpu); vcpu_load(vcpu);
if (!vcpu) spin_lock(&vcpu->kvm->lock);
return -ENOENT;
spin_lock(&kvm->lock);
gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
tr->physical_address = gpa; tr->physical_address = gpa;
tr->valid = gpa != UNMAPPED_GVA; tr->valid = gpa != UNMAPPED_GVA;
tr->writeable = 1; tr->writeable = 1;
tr->usermode = 0; tr->usermode = 0;
spin_unlock(&kvm->lock); spin_unlock(&vcpu->kvm->lock);
vcpu_put(vcpu); vcpu_put(vcpu);
return 0; return 0;
} }
static int kvm_vm_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq) static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
struct kvm_interrupt *irq)
{ {
struct kvm_vcpu *vcpu;
if (!valid_vcpu(irq->vcpu))
return -EINVAL;
if (irq->irq < 0 || irq->irq >= 256) if (irq->irq < 0 || irq->irq >= 256)
return -EINVAL; return -EINVAL;
vcpu = vcpu_load(kvm, irq->vcpu); vcpu_load(vcpu);
if (!vcpu)
return -ENOENT;
set_bit(irq->irq, vcpu->irq_pending); set_bit(irq->irq, vcpu->irq_pending);
set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
...@@ -1894,17 +1865,12 @@ static int kvm_vm_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq) ...@@ -1894,17 +1865,12 @@ static int kvm_vm_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq)
return 0; return 0;
} }
static int kvm_vm_ioctl_debug_guest(struct kvm *kvm, static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
struct kvm_debug_guest *dbg) struct kvm_debug_guest *dbg)
{ {
struct kvm_vcpu *vcpu;
int r; int r;
if (!valid_vcpu(dbg->vcpu)) vcpu_load(vcpu);
return -EINVAL;
vcpu = vcpu_load(kvm, dbg->vcpu);
if (!vcpu)
return -ENOENT;
r = kvm_arch_ops->set_guest_debug(vcpu, dbg); r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
...@@ -1913,6 +1879,59 @@ static int kvm_vm_ioctl_debug_guest(struct kvm *kvm, ...@@ -1913,6 +1879,59 @@ static int kvm_vm_ioctl_debug_guest(struct kvm *kvm,
return r; return r;
} }
static int kvm_vcpu_release(struct inode *inode, struct file *filp)
{
struct kvm_vcpu *vcpu = filp->private_data;
fput(vcpu->kvm->filp);
return 0;
}
static struct file_operations kvm_vcpu_fops = {
.release = kvm_vcpu_release,
.unlocked_ioctl = kvm_vcpu_ioctl,
.compat_ioctl = kvm_vcpu_ioctl,
};
/*
* Allocates an inode for the vcpu.
*/
static int create_vcpu_fd(struct kvm_vcpu *vcpu)
{
int fd, r;
struct inode *inode;
struct file *file;
atomic_inc(&vcpu->kvm->filp->f_count);
inode = kvmfs_inode(&kvm_vcpu_fops);
if (IS_ERR(inode)) {
r = PTR_ERR(inode);
goto out1;
}
file = kvmfs_file(inode, vcpu);
if (IS_ERR(file)) {
r = PTR_ERR(file);
goto out2;
}
r = get_unused_fd();
if (r < 0)
goto out3;
fd = r;
fd_install(fd, file);
return fd;
out3:
fput(file);
out2:
iput(inode);
out1:
fput(vcpu->kvm->filp);
return r;
}
/* /*
* Creates some virtual cpus. Good luck creating more than one. * Creates some virtual cpus. Good luck creating more than one.
*/ */
...@@ -1955,7 +1974,11 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) ...@@ -1955,7 +1974,11 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
if (r < 0) if (r < 0)
goto out_free_vcpus; goto out_free_vcpus;
return 0; r = create_vcpu_fd(vcpu);
if (r < 0)
goto out_free_vcpus;
return r;
out_free_vcpus: out_free_vcpus:
kvm_free_vcpu(vcpu); kvm_free_vcpu(vcpu);
...@@ -1964,26 +1987,21 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) ...@@ -1964,26 +1987,21 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
return r; return r;
} }
static long kvm_vm_ioctl(struct file *filp, static long kvm_vcpu_ioctl(struct file *filp,
unsigned int ioctl, unsigned long arg) unsigned int ioctl, unsigned long arg)
{ {
struct kvm *kvm = filp->private_data; struct kvm_vcpu *vcpu = filp->private_data;
void __user *argp = (void __user *)arg; void __user *argp = (void __user *)arg;
int r = -EINVAL; int r = -EINVAL;
switch (ioctl) { switch (ioctl) {
case KVM_CREATE_VCPU:
r = kvm_vm_ioctl_create_vcpu(kvm, arg);
if (r)
goto out;
break;
case KVM_RUN: { case KVM_RUN: {
struct kvm_run kvm_run; struct kvm_run kvm_run;
r = -EFAULT; r = -EFAULT;
if (copy_from_user(&kvm_run, argp, sizeof kvm_run)) if (copy_from_user(&kvm_run, argp, sizeof kvm_run))
goto out; goto out;
r = kvm_vm_ioctl_run(kvm, &kvm_run); r = kvm_vcpu_ioctl_run(vcpu, &kvm_run);
if (r < 0 && r != -EINTR) if (r < 0 && r != -EINTR)
goto out; goto out;
if (copy_to_user(argp, &kvm_run, sizeof kvm_run)) { if (copy_to_user(argp, &kvm_run, sizeof kvm_run)) {
...@@ -1995,10 +2013,8 @@ static long kvm_vm_ioctl(struct file *filp, ...@@ -1995,10 +2013,8 @@ static long kvm_vm_ioctl(struct file *filp,
case KVM_GET_REGS: { case KVM_GET_REGS: {
struct kvm_regs kvm_regs; struct kvm_regs kvm_regs;
r = -EFAULT; memset(&kvm_regs, 0, sizeof kvm_regs);
if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
goto out;
r = kvm_vm_ioctl_get_regs(kvm, &kvm_regs);
if (r) if (r)
goto out; goto out;
r = -EFAULT; r = -EFAULT;
...@@ -2013,7 +2029,7 @@ static long kvm_vm_ioctl(struct file *filp, ...@@ -2013,7 +2029,7 @@ static long kvm_vm_ioctl(struct file *filp,
r = -EFAULT; r = -EFAULT;
if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
goto out; goto out;
r = kvm_vm_ioctl_set_regs(kvm, &kvm_regs); r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
if (r) if (r)
goto out; goto out;
r = 0; r = 0;
...@@ -2022,10 +2038,8 @@ static long kvm_vm_ioctl(struct file *filp, ...@@ -2022,10 +2038,8 @@ static long kvm_vm_ioctl(struct file *filp,
case KVM_GET_SREGS: { case KVM_GET_SREGS: {
struct kvm_sregs kvm_sregs; struct kvm_sregs kvm_sregs;
r = -EFAULT; memset(&kvm_sregs, 0, sizeof kvm_sregs);
if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
goto out;
r = kvm_vm_ioctl_get_sregs(kvm, &kvm_sregs);
if (r) if (r)
goto out; goto out;
r = -EFAULT; r = -EFAULT;
...@@ -2040,7 +2054,7 @@ static long kvm_vm_ioctl(struct file *filp, ...@@ -2040,7 +2054,7 @@ static long kvm_vm_ioctl(struct file *filp,
r = -EFAULT; r = -EFAULT;
if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
goto out; goto out;
r = kvm_vm_ioctl_set_sregs(kvm, &kvm_sregs); r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
if (r) if (r)
goto out; goto out;
r = 0; r = 0;
...@@ -2052,7 +2066,7 @@ static long kvm_vm_ioctl(struct file *filp, ...@@ -2052,7 +2066,7 @@ static long kvm_vm_ioctl(struct file *filp,
r = -EFAULT; r = -EFAULT;
if (copy_from_user(&tr, argp, sizeof tr)) if (copy_from_user(&tr, argp, sizeof tr))
goto out; goto out;
r = kvm_vm_ioctl_translate(kvm, &tr); r = kvm_vcpu_ioctl_translate(vcpu, &tr);
if (r) if (r)
goto out; goto out;
r = -EFAULT; r = -EFAULT;
...@@ -2067,7 +2081,7 @@ static long kvm_vm_ioctl(struct file *filp, ...@@ -2067,7 +2081,7 @@ static long kvm_vm_ioctl(struct file *filp,
r = -EFAULT; r = -EFAULT;
if (copy_from_user(&irq, argp, sizeof irq)) if (copy_from_user(&irq, argp, sizeof irq))
goto out; goto out;
r = kvm_vm_ioctl_interrupt(kvm, &irq); r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
if (r) if (r)
goto out; goto out;
r = 0; r = 0;
...@@ -2079,12 +2093,38 @@ static long kvm_vm_ioctl(struct file *filp, ...@@ -2079,12 +2093,38 @@ static long kvm_vm_ioctl(struct file *filp,
r = -EFAULT; r = -EFAULT;
if (copy_from_user(&dbg, argp, sizeof dbg)) if (copy_from_user(&dbg, argp, sizeof dbg))
goto out; goto out;
r = kvm_vm_ioctl_debug_guest(kvm, &dbg); r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
if (r) if (r)
goto out; goto out;
r = 0; r = 0;
break; break;
} }
case KVM_GET_MSRS:
r = msr_io(vcpu, argp, get_msr, 1);
break;
case KVM_SET_MSRS:
r = msr_io(vcpu, argp, do_set_msr, 0);
break;
default:
;
}
out:
return r;
}
static long kvm_vm_ioctl(struct file *filp,
unsigned int ioctl, unsigned long arg)
{
struct kvm *kvm = filp->private_data;
void __user *argp = (void __user *)arg;
int r = -EINVAL;
switch (ioctl) {
case KVM_CREATE_VCPU:
r = kvm_vm_ioctl_create_vcpu(kvm, arg);
if (r < 0)
goto out;
break;
case KVM_SET_MEMORY_REGION: { case KVM_SET_MEMORY_REGION: {
struct kvm_memory_region kvm_mem; struct kvm_memory_region kvm_mem;
...@@ -2107,12 +2147,6 @@ static long kvm_vm_ioctl(struct file *filp, ...@@ -2107,12 +2147,6 @@ static long kvm_vm_ioctl(struct file *filp,
goto out; goto out;
break; break;
} }
case KVM_GET_MSRS:
r = msr_io(kvm, argp, get_msr, 1);
break;
case KVM_SET_MSRS:
r = msr_io(kvm, argp, do_set_msr, 0);
break;
default: default:
; ;
} }
...@@ -2182,6 +2216,7 @@ static int kvm_dev_ioctl_create_vm(void) ...@@ -2182,6 +2216,7 @@ static int kvm_dev_ioctl_create_vm(void)
r = PTR_ERR(file); r = PTR_ERR(file);
goto out3; goto out3;
} }
kvm->filp = file;
r = get_unused_fd(); r = get_unused_fd();
if (r < 0) if (r < 0)
......
...@@ -600,10 +600,9 @@ static void svm_free_vcpu(struct kvm_vcpu *vcpu) ...@@ -600,10 +600,9 @@ static void svm_free_vcpu(struct kvm_vcpu *vcpu)
kfree(vcpu->svm); kfree(vcpu->svm);
} }
static struct kvm_vcpu *svm_vcpu_load(struct kvm_vcpu *vcpu) static void svm_vcpu_load(struct kvm_vcpu *vcpu)
{ {
get_cpu(); get_cpu();
return vcpu;
} }
static void svm_vcpu_put(struct kvm_vcpu *vcpu) static void svm_vcpu_put(struct kvm_vcpu *vcpu)
......
...@@ -204,7 +204,7 @@ static void vmcs_write64(unsigned long field, u64 value) ...@@ -204,7 +204,7 @@ static void vmcs_write64(unsigned long field, u64 value)
* Switches to specified vcpu, until a matching vcpu_put(), but assumes * Switches to specified vcpu, until a matching vcpu_put(), but assumes
* vcpu mutex is already taken. * vcpu mutex is already taken.
*/ */
static struct kvm_vcpu *vmx_vcpu_load(struct kvm_vcpu *vcpu) static void vmx_vcpu_load(struct kvm_vcpu *vcpu)
{ {
u64 phys_addr = __pa(vcpu->vmcs); u64 phys_addr = __pa(vcpu->vmcs);
int cpu; int cpu;
...@@ -242,7 +242,6 @@ static struct kvm_vcpu *vmx_vcpu_load(struct kvm_vcpu *vcpu) ...@@ -242,7 +242,6 @@ static struct kvm_vcpu *vmx_vcpu_load(struct kvm_vcpu *vcpu)
rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
} }
return vcpu;
} }
static void vmx_vcpu_put(struct kvm_vcpu *vcpu) static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
......
...@@ -52,11 +52,10 @@ enum kvm_exit_reason { ...@@ -52,11 +52,10 @@ enum kvm_exit_reason {
/* for KVM_RUN */ /* for KVM_RUN */
struct kvm_run { struct kvm_run {
/* in */ /* in */
__u32 vcpu;
__u32 emulated; /* skip current instruction */ __u32 emulated; /* skip current instruction */
__u32 mmio_completed; /* mmio request completed */ __u32 mmio_completed; /* mmio request completed */
__u8 request_interrupt_window; __u8 request_interrupt_window;
__u8 padding1[3]; __u8 padding1[7];
/* out */ /* out */
__u32 exit_type; __u32 exit_type;
...@@ -111,10 +110,6 @@ struct kvm_run { ...@@ -111,10 +110,6 @@ struct kvm_run {
/* for KVM_GET_REGS and KVM_SET_REGS */ /* for KVM_GET_REGS and KVM_SET_REGS */
struct kvm_regs { struct kvm_regs {
/* in */
__u32 vcpu;
__u32 padding;
/* out (KVM_GET_REGS) / in (KVM_SET_REGS) */ /* out (KVM_GET_REGS) / in (KVM_SET_REGS) */
__u64 rax, rbx, rcx, rdx; __u64 rax, rbx, rcx, rdx;
__u64 rsi, rdi, rsp, rbp; __u64 rsi, rdi, rsp, rbp;
...@@ -141,10 +136,6 @@ struct kvm_dtable { ...@@ -141,10 +136,6 @@ struct kvm_dtable {
/* for KVM_GET_SREGS and KVM_SET_SREGS */ /* for KVM_GET_SREGS and KVM_SET_SREGS */
struct kvm_sregs { struct kvm_sregs {
/* in */
__u32 vcpu;
__u32 padding;
/* out (KVM_GET_SREGS) / in (KVM_SET_SREGS) */ /* out (KVM_GET_SREGS) / in (KVM_SET_SREGS) */
struct kvm_segment cs, ds, es, fs, gs, ss; struct kvm_segment cs, ds, es, fs, gs, ss;
struct kvm_segment tr, ldt; struct kvm_segment tr, ldt;
...@@ -163,8 +154,8 @@ struct kvm_msr_entry { ...@@ -163,8 +154,8 @@ struct kvm_msr_entry {
/* for KVM_GET_MSRS and KVM_SET_MSRS */ /* for KVM_GET_MSRS and KVM_SET_MSRS */
struct kvm_msrs { struct kvm_msrs {
__u32 vcpu;
__u32 nmsrs; /* number of msrs in entries */ __u32 nmsrs; /* number of msrs in entries */
__u32 pad;
struct kvm_msr_entry entries[0]; struct kvm_msr_entry entries[0];
}; };
...@@ -179,8 +170,6 @@ struct kvm_msr_list { ...@@ -179,8 +170,6 @@ struct kvm_msr_list {
struct kvm_translation { struct kvm_translation {
/* in */ /* in */
__u64 linear_address; __u64 linear_address;
__u32 vcpu;
__u32 padding;
/* out */ /* out */
__u64 physical_address; __u64 physical_address;
...@@ -193,7 +182,6 @@ struct kvm_translation { ...@@ -193,7 +182,6 @@ struct kvm_translation {
/* for KVM_INTERRUPT */ /* for KVM_INTERRUPT */
struct kvm_interrupt { struct kvm_interrupt {
/* in */ /* in */
__u32 vcpu;
__u32 irq; __u32 irq;
}; };
...@@ -206,8 +194,8 @@ struct kvm_breakpoint { ...@@ -206,8 +194,8 @@ struct kvm_breakpoint {
/* for KVM_DEBUG_GUEST */ /* for KVM_DEBUG_GUEST */
struct kvm_debug_guest { struct kvm_debug_guest {
/* int */ /* int */
__u32 vcpu;
__u32 enabled; __u32 enabled;
__u32 pad;
struct kvm_breakpoint breakpoints[4]; struct kvm_breakpoint breakpoints[4];
__u32 singlestep; __u32 singlestep;
}; };
...@@ -234,18 +222,26 @@ struct kvm_dirty_log { ...@@ -234,18 +222,26 @@ struct kvm_dirty_log {
/* /*
* ioctls for VM fds * ioctls for VM fds
*/ */
#define KVM_SET_MEMORY_REGION _IOW(KVMIO, 10, struct kvm_memory_region)
/*
* KVM_CREATE_VCPU receives as a parameter the vcpu slot, and returns
* a vcpu fd.
*/
#define KVM_CREATE_VCPU _IOW(KVMIO, 11, int)
#define KVM_GET_DIRTY_LOG _IOW(KVMIO, 12, struct kvm_dirty_log)
/*
* ioctls for vcpu fds
*/
#define KVM_RUN _IOWR(KVMIO, 2, struct kvm_run) #define KVM_RUN _IOWR(KVMIO, 2, struct kvm_run)
#define KVM_GET_REGS _IOWR(KVMIO, 3, struct kvm_regs) #define KVM_GET_REGS _IOR(KVMIO, 3, struct kvm_regs)
#define KVM_SET_REGS _IOW(KVMIO, 4, struct kvm_regs) #define KVM_SET_REGS _IOW(KVMIO, 4, struct kvm_regs)
#define KVM_GET_SREGS _IOWR(KVMIO, 5, struct kvm_sregs) #define KVM_GET_SREGS _IOR(KVMIO, 5, struct kvm_sregs)
#define KVM_SET_SREGS _IOW(KVMIO, 6, struct kvm_sregs) #define KVM_SET_SREGS _IOW(KVMIO, 6, struct kvm_sregs)
#define KVM_TRANSLATE _IOWR(KVMIO, 7, struct kvm_translation) #define KVM_TRANSLATE _IOWR(KVMIO, 7, struct kvm_translation)
#define KVM_INTERRUPT _IOW(KVMIO, 8, struct kvm_interrupt) #define KVM_INTERRUPT _IOW(KVMIO, 8, struct kvm_interrupt)
#define KVM_DEBUG_GUEST _IOW(KVMIO, 9, struct kvm_debug_guest) #define KVM_DEBUG_GUEST _IOW(KVMIO, 9, struct kvm_debug_guest)
#define KVM_SET_MEMORY_REGION _IOW(KVMIO, 10, struct kvm_memory_region)
#define KVM_CREATE_VCPU _IOW(KVMIO, 11, int /* vcpu_slot */)
#define KVM_GET_DIRTY_LOG _IOW(KVMIO, 12, struct kvm_dirty_log)
#define KVM_GET_MSRS _IOWR(KVMIO, 13, struct kvm_msrs) #define KVM_GET_MSRS _IOWR(KVMIO, 13, struct kvm_msrs)
#define KVM_SET_MSRS _IOWR(KVMIO, 14, struct kvm_msrs) #define KVM_SET_MSRS _IOW(KVMIO, 14, struct kvm_msrs)
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment