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: Avi Kivity <avi@qumranet.com>

+172 -142
+2 -1
drivers/kvm/kvm.h
··· 309 int busy; 310 unsigned long rmap_overflow; 311 struct list_head vm_list; 312 }; 313 314 struct kvm_stat { ··· 344 int (*vcpu_create)(struct kvm_vcpu *vcpu); 345 void (*vcpu_free)(struct kvm_vcpu *vcpu); 346 347 - struct kvm_vcpu *(*vcpu_load)(struct kvm_vcpu *vcpu); 348 void (*vcpu_put)(struct kvm_vcpu *vcpu); 349 void (*vcpu_decache)(struct kvm_vcpu *vcpu); 350
··· 309 int busy; 310 unsigned long rmap_overflow; 311 struct list_head vm_list; 312 + struct file *filp; 313 }; 314 315 struct kvm_stat { ··· 343 int (*vcpu_create)(struct kvm_vcpu *vcpu); 344 void (*vcpu_free)(struct kvm_vcpu *vcpu); 345 346 + void (*vcpu_load)(struct kvm_vcpu *vcpu); 347 void (*vcpu_put)(struct kvm_vcpu *vcpu); 348 void (*vcpu_decache)(struct kvm_vcpu *vcpu); 349
+151 -116
drivers/kvm/kvm_main.c
··· 96 97 #endif 98 99 static struct inode *kvmfs_inode(struct file_operations *fops) 100 { 101 int error = -ENOMEM; ··· 249 } 250 EXPORT_SYMBOL_GPL(kvm_write_guest); 251 252 - static int vcpu_slot(struct kvm_vcpu *vcpu) 253 - { 254 - return vcpu - vcpu->kvm->vcpus; 255 - } 256 - 257 /* 258 * Switches to specified vcpu, until a matching vcpu_put() 259 */ 260 - static struct kvm_vcpu *vcpu_load(struct kvm *kvm, int vcpu_slot) 261 { 262 - struct kvm_vcpu *vcpu = &kvm->vcpus[vcpu_slot]; 263 264 mutex_lock(&vcpu->mutex); 265 - if (unlikely(!vcpu->vmcs)) { 266 mutex_unlock(&vcpu->mutex); 267 return NULL; 268 } 269 - return kvm_arch_ops->vcpu_load(vcpu); 270 } 271 272 static void vcpu_put(struct kvm_vcpu *vcpu) ··· 345 346 static void kvm_free_vcpu(struct kvm_vcpu *vcpu) 347 { 348 - if (!vcpu_load(vcpu->kvm, vcpu_slot(vcpu))) 349 return; 350 351 kvm_mmu_destroy(vcpu); 352 vcpu_put(vcpu); 353 kvm_arch_ops->vcpu_free(vcpu); ··· 735 for (i = 0; i < KVM_MAX_VCPUS; ++i) { 736 struct kvm_vcpu *vcpu; 737 738 - vcpu = vcpu_load(kvm, i); 739 if (!vcpu) 740 continue; 741 kvm_mmu_reset_context(vcpu); ··· 801 if (any) { 802 cleared = 0; 803 for (i = 0; i < KVM_MAX_VCPUS; ++i) { 804 - struct kvm_vcpu *vcpu = vcpu_load(kvm, i); 805 806 if (!vcpu) 807 continue; 808 if (!cleared) { ··· 1472 { 1473 vcpu_put(vcpu); 1474 cond_resched(); 1475 - /* Cannot fail - no vcpu unplug yet. */ 1476 - vcpu_load(vcpu->kvm, vcpu_slot(vcpu)); 1477 } 1478 EXPORT_SYMBOL_GPL(kvm_resched); 1479 ··· 1494 } 1495 EXPORT_SYMBOL_GPL(save_msrs); 1496 1497 - static int kvm_vm_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run) 1498 { 1499 - struct kvm_vcpu *vcpu; 1500 int r; 1501 1502 - if (!valid_vcpu(kvm_run->vcpu)) 1503 - return -EINVAL; 1504 - 1505 - vcpu = vcpu_load(kvm, kvm_run->vcpu); 1506 - if (!vcpu) 1507 - return -ENOENT; 1508 1509 /* re-sync apic's tpr */ 1510 vcpu->cr8 = kvm_run->cr8; ··· 1521 return r; 1522 } 1523 1524 - static int kvm_vm_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs) 1525 { 1526 - struct kvm_vcpu *vcpu; 1527 - 1528 - if (!valid_vcpu(regs->vcpu)) 1529 - return -EINVAL; 1530 - 1531 - vcpu = vcpu_load(kvm, regs->vcpu); 1532 - if (!vcpu) 1533 - return -ENOENT; 1534 1535 kvm_arch_ops->cache_regs(vcpu); 1536 ··· 1561 return 0; 1562 } 1563 1564 - static int kvm_vm_ioctl_set_regs(struct kvm *kvm, struct kvm_regs *regs) 1565 { 1566 - struct kvm_vcpu *vcpu; 1567 - 1568 - if (!valid_vcpu(regs->vcpu)) 1569 - return -EINVAL; 1570 - 1571 - vcpu = vcpu_load(kvm, regs->vcpu); 1572 - if (!vcpu) 1573 - return -ENOENT; 1574 1575 vcpu->regs[VCPU_REGS_RAX] = regs->rax; 1576 vcpu->regs[VCPU_REGS_RBX] = regs->rbx; ··· 1601 return kvm_arch_ops->get_segment(vcpu, var, seg); 1602 } 1603 1604 - static int kvm_vm_ioctl_get_sregs(struct kvm *kvm, struct kvm_sregs *sregs) 1605 { 1606 - struct kvm_vcpu *vcpu; 1607 struct descriptor_table dt; 1608 1609 - if (!valid_vcpu(sregs->vcpu)) 1610 - return -EINVAL; 1611 - vcpu = vcpu_load(kvm, sregs->vcpu); 1612 - if (!vcpu) 1613 - return -ENOENT; 1614 1615 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); 1616 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); ··· 1648 return kvm_arch_ops->set_segment(vcpu, var, seg); 1649 } 1650 1651 - static int kvm_vm_ioctl_set_sregs(struct kvm *kvm, struct kvm_sregs *sregs) 1652 { 1653 - struct kvm_vcpu *vcpu; 1654 int mmu_reset_needed = 0; 1655 int i; 1656 struct descriptor_table dt; 1657 1658 - if (!valid_vcpu(sregs->vcpu)) 1659 - return -EINVAL; 1660 - vcpu = vcpu_load(kvm, sregs->vcpu); 1661 - if (!vcpu) 1662 - return -ENOENT; 1663 1664 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); 1665 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); ··· 1761 * 1762 * @return number of msrs set successfully. 1763 */ 1764 - static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs, 1765 struct kvm_msr_entry *entries, 1766 int (*do_msr)(struct kvm_vcpu *vcpu, 1767 unsigned index, u64 *data)) 1768 { 1769 - struct kvm_vcpu *vcpu; 1770 int i; 1771 1772 - if (!valid_vcpu(msrs->vcpu)) 1773 - return -EINVAL; 1774 - 1775 - vcpu = vcpu_load(kvm, msrs->vcpu); 1776 - if (!vcpu) 1777 - return -ENOENT; 1778 1779 for (i = 0; i < msrs->nmsrs; ++i) 1780 if (do_msr(vcpu, entries[i].index, &entries[i].data)) ··· 1784 * 1785 * @return number of msrs set successfully. 1786 */ 1787 - static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs, 1788 int (*do_msr)(struct kvm_vcpu *vcpu, 1789 unsigned index, u64 *data), 1790 int writeback) ··· 1812 if (copy_from_user(entries, user_msrs->entries, size)) 1813 goto out_free; 1814 1815 - r = n = __msr_io(kvm, &msrs, entries, do_msr); 1816 if (r < 0) 1817 goto out_free; 1818 ··· 1831 /* 1832 * Translate a guest virtual address to a guest physical address. 1833 */ 1834 - static int kvm_vm_ioctl_translate(struct kvm *kvm, struct kvm_translation *tr) 1835 { 1836 unsigned long vaddr = tr->linear_address; 1837 - struct kvm_vcpu *vcpu; 1838 gpa_t gpa; 1839 1840 - vcpu = vcpu_load(kvm, tr->vcpu); 1841 - if (!vcpu) 1842 - return -ENOENT; 1843 - spin_lock(&kvm->lock); 1844 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); 1845 tr->physical_address = gpa; 1846 tr->valid = gpa != UNMAPPED_GVA; 1847 tr->writeable = 1; 1848 tr->usermode = 0; 1849 - spin_unlock(&kvm->lock); 1850 vcpu_put(vcpu); 1851 1852 return 0; 1853 } 1854 1855 - static int kvm_vm_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq) 1856 { 1857 - struct kvm_vcpu *vcpu; 1858 - 1859 - if (!valid_vcpu(irq->vcpu)) 1860 - return -EINVAL; 1861 if (irq->irq < 0 || irq->irq >= 256) 1862 return -EINVAL; 1863 - vcpu = vcpu_load(kvm, irq->vcpu); 1864 - if (!vcpu) 1865 - return -ENOENT; 1866 1867 set_bit(irq->irq, vcpu->irq_pending); 1868 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); ··· 1865 return 0; 1866 } 1867 1868 - static int kvm_vm_ioctl_debug_guest(struct kvm *kvm, 1869 - struct kvm_debug_guest *dbg) 1870 { 1871 - struct kvm_vcpu *vcpu; 1872 int r; 1873 1874 - if (!valid_vcpu(dbg->vcpu)) 1875 - return -EINVAL; 1876 - vcpu = vcpu_load(kvm, dbg->vcpu); 1877 - if (!vcpu) 1878 - return -ENOENT; 1879 1880 r = kvm_arch_ops->set_guest_debug(vcpu, dbg); 1881 1882 vcpu_put(vcpu); 1883 1884 return r; 1885 } 1886 ··· 1974 if (r < 0) 1975 goto out_free_vcpus; 1976 1977 - return 0; 1978 1979 out_free_vcpus: 1980 kvm_free_vcpu(vcpu); ··· 1987 return r; 1988 } 1989 1990 - static long kvm_vm_ioctl(struct file *filp, 1991 - unsigned int ioctl, unsigned long arg) 1992 { 1993 - struct kvm *kvm = filp->private_data; 1994 void __user *argp = (void __user *)arg; 1995 int r = -EINVAL; 1996 1997 switch (ioctl) { 1998 - case KVM_CREATE_VCPU: 1999 - r = kvm_vm_ioctl_create_vcpu(kvm, arg); 2000 - if (r) 2001 - goto out; 2002 - break; 2003 case KVM_RUN: { 2004 struct kvm_run kvm_run; 2005 2006 r = -EFAULT; 2007 if (copy_from_user(&kvm_run, argp, sizeof kvm_run)) 2008 goto out; 2009 - r = kvm_vm_ioctl_run(kvm, &kvm_run); 2010 if (r < 0 && r != -EINTR) 2011 goto out; 2012 if (copy_to_user(argp, &kvm_run, sizeof kvm_run)) { ··· 2013 case KVM_GET_REGS: { 2014 struct kvm_regs kvm_regs; 2015 2016 - r = -EFAULT; 2017 - if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) 2018 - goto out; 2019 - r = kvm_vm_ioctl_get_regs(kvm, &kvm_regs); 2020 if (r) 2021 goto out; 2022 r = -EFAULT; ··· 2029 r = -EFAULT; 2030 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) 2031 goto out; 2032 - r = kvm_vm_ioctl_set_regs(kvm, &kvm_regs); 2033 if (r) 2034 goto out; 2035 r = 0; ··· 2038 case KVM_GET_SREGS: { 2039 struct kvm_sregs kvm_sregs; 2040 2041 - r = -EFAULT; 2042 - if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) 2043 - goto out; 2044 - r = kvm_vm_ioctl_get_sregs(kvm, &kvm_sregs); 2045 if (r) 2046 goto out; 2047 r = -EFAULT; ··· 2054 r = -EFAULT; 2055 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) 2056 goto out; 2057 - r = kvm_vm_ioctl_set_sregs(kvm, &kvm_sregs); 2058 if (r) 2059 goto out; 2060 r = 0; ··· 2066 r = -EFAULT; 2067 if (copy_from_user(&tr, argp, sizeof tr)) 2068 goto out; 2069 - r = kvm_vm_ioctl_translate(kvm, &tr); 2070 if (r) 2071 goto out; 2072 r = -EFAULT; ··· 2081 r = -EFAULT; 2082 if (copy_from_user(&irq, argp, sizeof irq)) 2083 goto out; 2084 - r = kvm_vm_ioctl_interrupt(kvm, &irq); 2085 if (r) 2086 goto out; 2087 r = 0; ··· 2093 r = -EFAULT; 2094 if (copy_from_user(&dbg, argp, sizeof dbg)) 2095 goto out; 2096 - r = kvm_vm_ioctl_debug_guest(kvm, &dbg); 2097 if (r) 2098 goto out; 2099 r = 0; 2100 break; 2101 } 2102 case KVM_SET_MEMORY_REGION: { 2103 struct kvm_memory_region kvm_mem; 2104 ··· 2147 goto out; 2148 break; 2149 } 2150 - case KVM_GET_MSRS: 2151 - r = msr_io(kvm, argp, get_msr, 1); 2152 - break; 2153 - case KVM_SET_MSRS: 2154 - r = msr_io(kvm, argp, do_set_msr, 0); 2155 - break; 2156 default: 2157 ; 2158 } ··· 2216 r = PTR_ERR(file); 2217 goto out3; 2218 } 2219 2220 r = get_unused_fd(); 2221 if (r < 0)
··· 96 97 #endif 98 99 + static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, 100 + unsigned long arg); 101 + 102 static struct inode *kvmfs_inode(struct file_operations *fops) 103 { 104 int error = -ENOMEM; ··· 246 } 247 EXPORT_SYMBOL_GPL(kvm_write_guest); 248 249 /* 250 * Switches to specified vcpu, until a matching vcpu_put() 251 */ 252 + static void vcpu_load(struct kvm_vcpu *vcpu) 253 { 254 + mutex_lock(&vcpu->mutex); 255 + kvm_arch_ops->vcpu_load(vcpu); 256 + } 257 + 258 + /* 259 + * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL 260 + * if the slot is not populated. 261 + */ 262 + static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot) 263 + { 264 + struct kvm_vcpu *vcpu = &kvm->vcpus[slot]; 265 266 mutex_lock(&vcpu->mutex); 267 + if (!vcpu->vmcs) { 268 mutex_unlock(&vcpu->mutex); 269 return NULL; 270 } 271 + kvm_arch_ops->vcpu_load(vcpu); 272 + return vcpu; 273 } 274 275 static void vcpu_put(struct kvm_vcpu *vcpu) ··· 336 337 static void kvm_free_vcpu(struct kvm_vcpu *vcpu) 338 { 339 + if (!vcpu->vmcs) 340 return; 341 342 + vcpu_load(vcpu); 343 kvm_mmu_destroy(vcpu); 344 vcpu_put(vcpu); 345 kvm_arch_ops->vcpu_free(vcpu); ··· 725 for (i = 0; i < KVM_MAX_VCPUS; ++i) { 726 struct kvm_vcpu *vcpu; 727 728 + vcpu = vcpu_load_slot(kvm, i); 729 if (!vcpu) 730 continue; 731 kvm_mmu_reset_context(vcpu); ··· 791 if (any) { 792 cleared = 0; 793 for (i = 0; i < KVM_MAX_VCPUS; ++i) { 794 + struct kvm_vcpu *vcpu; 795 796 + vcpu = vcpu_load_slot(kvm, i); 797 if (!vcpu) 798 continue; 799 if (!cleared) { ··· 1461 { 1462 vcpu_put(vcpu); 1463 cond_resched(); 1464 + vcpu_load(vcpu); 1465 } 1466 EXPORT_SYMBOL_GPL(kvm_resched); 1467 ··· 1484 } 1485 EXPORT_SYMBOL_GPL(save_msrs); 1486 1487 + static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 1488 { 1489 int r; 1490 1491 + vcpu_load(vcpu); 1492 1493 /* re-sync apic's tpr */ 1494 vcpu->cr8 = kvm_run->cr8; ··· 1517 return r; 1518 } 1519 1520 + static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, 1521 + struct kvm_regs *regs) 1522 { 1523 + vcpu_load(vcpu); 1524 1525 kvm_arch_ops->cache_regs(vcpu); 1526 ··· 1563 return 0; 1564 } 1565 1566 + static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, 1567 + struct kvm_regs *regs) 1568 { 1569 + vcpu_load(vcpu); 1570 1571 vcpu->regs[VCPU_REGS_RAX] = regs->rax; 1572 vcpu->regs[VCPU_REGS_RBX] = regs->rbx; ··· 1609 return kvm_arch_ops->get_segment(vcpu, var, seg); 1610 } 1611 1612 + static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 1613 + struct kvm_sregs *sregs) 1614 { 1615 struct descriptor_table dt; 1616 1617 + vcpu_load(vcpu); 1618 1619 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); 1620 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); ··· 1660 return kvm_arch_ops->set_segment(vcpu, var, seg); 1661 } 1662 1663 + static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 1664 + struct kvm_sregs *sregs) 1665 { 1666 int mmu_reset_needed = 0; 1667 int i; 1668 struct descriptor_table dt; 1669 1670 + vcpu_load(vcpu); 1671 1672 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); 1673 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); ··· 1777 * 1778 * @return number of msrs set successfully. 1779 */ 1780 + static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, 1781 struct kvm_msr_entry *entries, 1782 int (*do_msr)(struct kvm_vcpu *vcpu, 1783 unsigned index, u64 *data)) 1784 { 1785 int i; 1786 1787 + vcpu_load(vcpu); 1788 1789 for (i = 0; i < msrs->nmsrs; ++i) 1790 if (do_msr(vcpu, entries[i].index, &entries[i].data)) ··· 1806 * 1807 * @return number of msrs set successfully. 1808 */ 1809 + static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, 1810 int (*do_msr)(struct kvm_vcpu *vcpu, 1811 unsigned index, u64 *data), 1812 int writeback) ··· 1834 if (copy_from_user(entries, user_msrs->entries, size)) 1835 goto out_free; 1836 1837 + r = n = __msr_io(vcpu, &msrs, entries, do_msr); 1838 if (r < 0) 1839 goto out_free; 1840 ··· 1853 /* 1854 * Translate a guest virtual address to a guest physical address. 1855 */ 1856 + static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 1857 + struct kvm_translation *tr) 1858 { 1859 unsigned long vaddr = tr->linear_address; 1860 gpa_t gpa; 1861 1862 + vcpu_load(vcpu); 1863 + spin_lock(&vcpu->kvm->lock); 1864 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); 1865 tr->physical_address = gpa; 1866 tr->valid = gpa != UNMAPPED_GVA; 1867 tr->writeable = 1; 1868 tr->usermode = 0; 1869 + spin_unlock(&vcpu->kvm->lock); 1870 vcpu_put(vcpu); 1871 1872 return 0; 1873 } 1874 1875 + static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, 1876 + struct kvm_interrupt *irq) 1877 { 1878 if (irq->irq < 0 || irq->irq >= 256) 1879 return -EINVAL; 1880 + vcpu_load(vcpu); 1881 1882 set_bit(irq->irq, vcpu->irq_pending); 1883 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); ··· 1894 return 0; 1895 } 1896 1897 + static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, 1898 + struct kvm_debug_guest *dbg) 1899 { 1900 int r; 1901 1902 + vcpu_load(vcpu); 1903 1904 r = kvm_arch_ops->set_guest_debug(vcpu, dbg); 1905 1906 vcpu_put(vcpu); 1907 1908 + return r; 1909 + } 1910 + 1911 + static int kvm_vcpu_release(struct inode *inode, struct file *filp) 1912 + { 1913 + struct kvm_vcpu *vcpu = filp->private_data; 1914 + 1915 + fput(vcpu->kvm->filp); 1916 + return 0; 1917 + } 1918 + 1919 + static struct file_operations kvm_vcpu_fops = { 1920 + .release = kvm_vcpu_release, 1921 + .unlocked_ioctl = kvm_vcpu_ioctl, 1922 + .compat_ioctl = kvm_vcpu_ioctl, 1923 + }; 1924 + 1925 + /* 1926 + * Allocates an inode for the vcpu. 1927 + */ 1928 + static int create_vcpu_fd(struct kvm_vcpu *vcpu) 1929 + { 1930 + int fd, r; 1931 + struct inode *inode; 1932 + struct file *file; 1933 + 1934 + atomic_inc(&vcpu->kvm->filp->f_count); 1935 + inode = kvmfs_inode(&kvm_vcpu_fops); 1936 + if (IS_ERR(inode)) { 1937 + r = PTR_ERR(inode); 1938 + goto out1; 1939 + } 1940 + 1941 + file = kvmfs_file(inode, vcpu); 1942 + if (IS_ERR(file)) { 1943 + r = PTR_ERR(file); 1944 + goto out2; 1945 + } 1946 + 1947 + r = get_unused_fd(); 1948 + if (r < 0) 1949 + goto out3; 1950 + fd = r; 1951 + fd_install(fd, file); 1952 + 1953 + return fd; 1954 + 1955 + out3: 1956 + fput(file); 1957 + out2: 1958 + iput(inode); 1959 + out1: 1960 + fput(vcpu->kvm->filp); 1961 return r; 1962 } 1963 ··· 1955 if (r < 0) 1956 goto out_free_vcpus; 1957 1958 + r = create_vcpu_fd(vcpu); 1959 + if (r < 0) 1960 + goto out_free_vcpus; 1961 + 1962 + return r; 1963 1964 out_free_vcpus: 1965 kvm_free_vcpu(vcpu); ··· 1964 return r; 1965 } 1966 1967 + static long kvm_vcpu_ioctl(struct file *filp, 1968 + unsigned int ioctl, unsigned long arg) 1969 { 1970 + struct kvm_vcpu *vcpu = filp->private_data; 1971 void __user *argp = (void __user *)arg; 1972 int r = -EINVAL; 1973 1974 switch (ioctl) { 1975 case KVM_RUN: { 1976 struct kvm_run kvm_run; 1977 1978 r = -EFAULT; 1979 if (copy_from_user(&kvm_run, argp, sizeof kvm_run)) 1980 goto out; 1981 + r = kvm_vcpu_ioctl_run(vcpu, &kvm_run); 1982 if (r < 0 && r != -EINTR) 1983 goto out; 1984 if (copy_to_user(argp, &kvm_run, sizeof kvm_run)) { ··· 1995 case KVM_GET_REGS: { 1996 struct kvm_regs kvm_regs; 1997 1998 + memset(&kvm_regs, 0, sizeof kvm_regs); 1999 + r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs); 2000 if (r) 2001 goto out; 2002 r = -EFAULT; ··· 2013 r = -EFAULT; 2014 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) 2015 goto out; 2016 + r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs); 2017 if (r) 2018 goto out; 2019 r = 0; ··· 2022 case KVM_GET_SREGS: { 2023 struct kvm_sregs kvm_sregs; 2024 2025 + memset(&kvm_sregs, 0, sizeof kvm_sregs); 2026 + r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); 2027 if (r) 2028 goto out; 2029 r = -EFAULT; ··· 2040 r = -EFAULT; 2041 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) 2042 goto out; 2043 + r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); 2044 if (r) 2045 goto out; 2046 r = 0; ··· 2052 r = -EFAULT; 2053 if (copy_from_user(&tr, argp, sizeof tr)) 2054 goto out; 2055 + r = kvm_vcpu_ioctl_translate(vcpu, &tr); 2056 if (r) 2057 goto out; 2058 r = -EFAULT; ··· 2067 r = -EFAULT; 2068 if (copy_from_user(&irq, argp, sizeof irq)) 2069 goto out; 2070 + r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); 2071 if (r) 2072 goto out; 2073 r = 0; ··· 2079 r = -EFAULT; 2080 if (copy_from_user(&dbg, argp, sizeof dbg)) 2081 goto out; 2082 + r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg); 2083 if (r) 2084 goto out; 2085 r = 0; 2086 break; 2087 } 2088 + case KVM_GET_MSRS: 2089 + r = msr_io(vcpu, argp, get_msr, 1); 2090 + break; 2091 + case KVM_SET_MSRS: 2092 + r = msr_io(vcpu, argp, do_set_msr, 0); 2093 + break; 2094 + default: 2095 + ; 2096 + } 2097 + out: 2098 + return r; 2099 + } 2100 + 2101 + static long kvm_vm_ioctl(struct file *filp, 2102 + unsigned int ioctl, unsigned long arg) 2103 + { 2104 + struct kvm *kvm = filp->private_data; 2105 + void __user *argp = (void __user *)arg; 2106 + int r = -EINVAL; 2107 + 2108 + switch (ioctl) { 2109 + case KVM_CREATE_VCPU: 2110 + r = kvm_vm_ioctl_create_vcpu(kvm, arg); 2111 + if (r < 0) 2112 + goto out; 2113 + break; 2114 case KVM_SET_MEMORY_REGION: { 2115 struct kvm_memory_region kvm_mem; 2116 ··· 2107 goto out; 2108 break; 2109 } 2110 default: 2111 ; 2112 } ··· 2182 r = PTR_ERR(file); 2183 goto out3; 2184 } 2185 + kvm->filp = file; 2186 2187 r = get_unused_fd(); 2188 if (r < 0)
+1 -2
drivers/kvm/svm.c
··· 600 kfree(vcpu->svm); 601 } 602 603 - static struct kvm_vcpu *svm_vcpu_load(struct kvm_vcpu *vcpu) 604 { 605 get_cpu(); 606 - return vcpu; 607 } 608 609 static void svm_vcpu_put(struct kvm_vcpu *vcpu)
··· 600 kfree(vcpu->svm); 601 } 602 603 + static void svm_vcpu_load(struct kvm_vcpu *vcpu) 604 { 605 get_cpu(); 606 } 607 608 static void svm_vcpu_put(struct kvm_vcpu *vcpu)
+1 -2
drivers/kvm/vmx.c
··· 204 * Switches to specified vcpu, until a matching vcpu_put(), but assumes 205 * vcpu mutex is already taken. 206 */ 207 - static struct kvm_vcpu *vmx_vcpu_load(struct kvm_vcpu *vcpu) 208 { 209 u64 phys_addr = __pa(vcpu->vmcs); 210 int cpu; ··· 242 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); 243 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ 244 } 245 - return vcpu; 246 } 247 248 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
··· 204 * Switches to specified vcpu, until a matching vcpu_put(), but assumes 205 * vcpu mutex is already taken. 206 */ 207 + static void vmx_vcpu_load(struct kvm_vcpu *vcpu) 208 { 209 u64 phys_addr = __pa(vcpu->vmcs); 210 int cpu; ··· 242 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); 243 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ 244 } 245 } 246 247 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
+17 -21
include/linux/kvm.h
··· 52 /* for KVM_RUN */ 53 struct kvm_run { 54 /* in */ 55 - __u32 vcpu; 56 __u32 emulated; /* skip current instruction */ 57 __u32 mmio_completed; /* mmio request completed */ 58 __u8 request_interrupt_window; 59 - __u8 padding1[3]; 60 61 /* out */ 62 __u32 exit_type; ··· 110 111 /* for KVM_GET_REGS and KVM_SET_REGS */ 112 struct kvm_regs { 113 - /* in */ 114 - __u32 vcpu; 115 - __u32 padding; 116 - 117 /* out (KVM_GET_REGS) / in (KVM_SET_REGS) */ 118 __u64 rax, rbx, rcx, rdx; 119 __u64 rsi, rdi, rsp, rbp; ··· 136 137 /* for KVM_GET_SREGS and KVM_SET_SREGS */ 138 struct kvm_sregs { 139 - /* in */ 140 - __u32 vcpu; 141 - __u32 padding; 142 - 143 /* out (KVM_GET_SREGS) / in (KVM_SET_SREGS) */ 144 struct kvm_segment cs, ds, es, fs, gs, ss; 145 struct kvm_segment tr, ldt; ··· 154 155 /* for KVM_GET_MSRS and KVM_SET_MSRS */ 156 struct kvm_msrs { 157 - __u32 vcpu; 158 __u32 nmsrs; /* number of msrs in entries */ 159 160 struct kvm_msr_entry entries[0]; 161 }; ··· 170 struct kvm_translation { 171 /* in */ 172 __u64 linear_address; 173 - __u32 vcpu; 174 - __u32 padding; 175 176 /* out */ 177 __u64 physical_address; ··· 182 /* for KVM_INTERRUPT */ 183 struct kvm_interrupt { 184 /* in */ 185 - __u32 vcpu; 186 __u32 irq; 187 }; 188 ··· 194 /* for KVM_DEBUG_GUEST */ 195 struct kvm_debug_guest { 196 /* int */ 197 - __u32 vcpu; 198 __u32 enabled; 199 struct kvm_breakpoint breakpoints[4]; 200 __u32 singlestep; 201 }; ··· 222 /* 223 * ioctls for VM fds 224 */ 225 #define KVM_RUN _IOWR(KVMIO, 2, struct kvm_run) 226 - #define KVM_GET_REGS _IOWR(KVMIO, 3, struct kvm_regs) 227 #define KVM_SET_REGS _IOW(KVMIO, 4, struct kvm_regs) 228 - #define KVM_GET_SREGS _IOWR(KVMIO, 5, struct kvm_sregs) 229 #define KVM_SET_SREGS _IOW(KVMIO, 6, struct kvm_sregs) 230 #define KVM_TRANSLATE _IOWR(KVMIO, 7, struct kvm_translation) 231 #define KVM_INTERRUPT _IOW(KVMIO, 8, struct kvm_interrupt) 232 #define KVM_DEBUG_GUEST _IOW(KVMIO, 9, struct kvm_debug_guest) 233 - #define KVM_SET_MEMORY_REGION _IOW(KVMIO, 10, struct kvm_memory_region) 234 - #define KVM_CREATE_VCPU _IOW(KVMIO, 11, int /* vcpu_slot */) 235 - #define KVM_GET_DIRTY_LOG _IOW(KVMIO, 12, struct kvm_dirty_log) 236 #define KVM_GET_MSRS _IOWR(KVMIO, 13, struct kvm_msrs) 237 - #define KVM_SET_MSRS _IOWR(KVMIO, 14, struct kvm_msrs) 238 239 #endif
··· 52 /* for KVM_RUN */ 53 struct kvm_run { 54 /* in */ 55 __u32 emulated; /* skip current instruction */ 56 __u32 mmio_completed; /* mmio request completed */ 57 __u8 request_interrupt_window; 58 + __u8 padding1[7]; 59 60 /* out */ 61 __u32 exit_type; ··· 111 112 /* for KVM_GET_REGS and KVM_SET_REGS */ 113 struct kvm_regs { 114 /* out (KVM_GET_REGS) / in (KVM_SET_REGS) */ 115 __u64 rax, rbx, rcx, rdx; 116 __u64 rsi, rdi, rsp, rbp; ··· 141 142 /* for KVM_GET_SREGS and KVM_SET_SREGS */ 143 struct kvm_sregs { 144 /* out (KVM_GET_SREGS) / in (KVM_SET_SREGS) */ 145 struct kvm_segment cs, ds, es, fs, gs, ss; 146 struct kvm_segment tr, ldt; ··· 163 164 /* for KVM_GET_MSRS and KVM_SET_MSRS */ 165 struct kvm_msrs { 166 __u32 nmsrs; /* number of msrs in entries */ 167 + __u32 pad; 168 169 struct kvm_msr_entry entries[0]; 170 }; ··· 179 struct kvm_translation { 180 /* in */ 181 __u64 linear_address; 182 183 /* out */ 184 __u64 physical_address; ··· 193 /* for KVM_INTERRUPT */ 194 struct kvm_interrupt { 195 /* in */ 196 __u32 irq; 197 }; 198 ··· 206 /* for KVM_DEBUG_GUEST */ 207 struct kvm_debug_guest { 208 /* int */ 209 __u32 enabled; 210 + __u32 pad; 211 struct kvm_breakpoint breakpoints[4]; 212 __u32 singlestep; 213 }; ··· 234 /* 235 * ioctls for VM fds 236 */ 237 + #define KVM_SET_MEMORY_REGION _IOW(KVMIO, 10, struct kvm_memory_region) 238 + /* 239 + * KVM_CREATE_VCPU receives as a parameter the vcpu slot, and returns 240 + * a vcpu fd. 241 + */ 242 + #define KVM_CREATE_VCPU _IOW(KVMIO, 11, int) 243 + #define KVM_GET_DIRTY_LOG _IOW(KVMIO, 12, struct kvm_dirty_log) 244 + 245 + /* 246 + * ioctls for vcpu fds 247 + */ 248 #define KVM_RUN _IOWR(KVMIO, 2, struct kvm_run) 249 + #define KVM_GET_REGS _IOR(KVMIO, 3, struct kvm_regs) 250 #define KVM_SET_REGS _IOW(KVMIO, 4, struct kvm_regs) 251 + #define KVM_GET_SREGS _IOR(KVMIO, 5, struct kvm_sregs) 252 #define KVM_SET_SREGS _IOW(KVMIO, 6, struct kvm_sregs) 253 #define KVM_TRANSLATE _IOWR(KVMIO, 7, struct kvm_translation) 254 #define KVM_INTERRUPT _IOW(KVMIO, 8, struct kvm_interrupt) 255 #define KVM_DEBUG_GUEST _IOW(KVMIO, 9, struct kvm_debug_guest) 256 #define KVM_GET_MSRS _IOWR(KVMIO, 13, struct kvm_msrs) 257 + #define KVM_SET_MSRS _IOW(KVMIO, 14, struct kvm_msrs) 258 259 #endif