aboutsummaryrefslogtreecommitdiffstats
path: root/virt/kvm/kvm_main.c
diff options
context:
space:
mode:
Diffstat (limited to 'virt/kvm/kvm_main.c')
-rw-r--r--virt/kvm/kvm_main.c298
1 files changed, 206 insertions, 92 deletions
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 2884baf1d5f9..897bff3b7df9 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -59,9 +59,18 @@
59#include "irq.h" 59#include "irq.h"
60#endif 60#endif
61 61
62#define CREATE_TRACE_POINTS
63#include <trace/events/kvm.h>
64
62MODULE_AUTHOR("Qumranet"); 65MODULE_AUTHOR("Qumranet");
63MODULE_LICENSE("GPL"); 66MODULE_LICENSE("GPL");
64 67
68/*
69 * Ordering of locks:
70 *
71 * kvm->slots_lock --> kvm->lock --> kvm->irq_lock
72 */
73
65DEFINE_SPINLOCK(kvm_lock); 74DEFINE_SPINLOCK(kvm_lock);
66LIST_HEAD(vm_list); 75LIST_HEAD(vm_list);
67 76
@@ -79,6 +88,8 @@ static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
79 88
80static bool kvm_rebooting; 89static bool kvm_rebooting;
81 90
91static bool largepages_enabled = true;
92
82#ifdef KVM_CAP_DEVICE_ASSIGNMENT 93#ifdef KVM_CAP_DEVICE_ASSIGNMENT
83static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head, 94static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
84 int assigned_dev_id) 95 int assigned_dev_id)
@@ -120,17 +131,13 @@ static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
120{ 131{
121 struct kvm_assigned_dev_kernel *assigned_dev; 132 struct kvm_assigned_dev_kernel *assigned_dev;
122 struct kvm *kvm; 133 struct kvm *kvm;
123 int irq, i; 134 int i;
124 135
125 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel, 136 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
126 interrupt_work); 137 interrupt_work);
127 kvm = assigned_dev->kvm; 138 kvm = assigned_dev->kvm;
128 139
129 /* This is taken to safely inject irq inside the guest. When 140 mutex_lock(&kvm->irq_lock);
130 * the interrupt injection (or the ioapic code) uses a
131 * finer-grained lock, update this
132 */
133 mutex_lock(&kvm->lock);
134 spin_lock_irq(&assigned_dev->assigned_dev_lock); 141 spin_lock_irq(&assigned_dev->assigned_dev_lock);
135 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) { 142 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
136 struct kvm_guest_msix_entry *guest_entries = 143 struct kvm_guest_msix_entry *guest_entries =
@@ -143,23 +150,13 @@ static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
143 kvm_set_irq(assigned_dev->kvm, 150 kvm_set_irq(assigned_dev->kvm,
144 assigned_dev->irq_source_id, 151 assigned_dev->irq_source_id,
145 guest_entries[i].vector, 1); 152 guest_entries[i].vector, 1);
146 irq = assigned_dev->host_msix_entries[i].vector;
147 if (irq != 0)
148 enable_irq(irq);
149 assigned_dev->host_irq_disabled = false;
150 } 153 }
151 } else { 154 } else
152 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id, 155 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
153 assigned_dev->guest_irq, 1); 156 assigned_dev->guest_irq, 1);
154 if (assigned_dev->irq_requested_type &
155 KVM_DEV_IRQ_GUEST_MSI) {
156 enable_irq(assigned_dev->host_irq);
157 assigned_dev->host_irq_disabled = false;
158 }
159 }
160 157
161 spin_unlock_irq(&assigned_dev->assigned_dev_lock); 158 spin_unlock_irq(&assigned_dev->assigned_dev_lock);
162 mutex_unlock(&assigned_dev->kvm->lock); 159 mutex_unlock(&assigned_dev->kvm->irq_lock);
163} 160}
164 161
165static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id) 162static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
@@ -179,8 +176,10 @@ static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
179 176
180 schedule_work(&assigned_dev->interrupt_work); 177 schedule_work(&assigned_dev->interrupt_work);
181 178
182 disable_irq_nosync(irq); 179 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) {
183 assigned_dev->host_irq_disabled = true; 180 disable_irq_nosync(irq);
181 assigned_dev->host_irq_disabled = true;
182 }
184 183
185out: 184out:
186 spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags); 185 spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags);
@@ -215,7 +214,7 @@ static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
215static void deassign_guest_irq(struct kvm *kvm, 214static void deassign_guest_irq(struct kvm *kvm,
216 struct kvm_assigned_dev_kernel *assigned_dev) 215 struct kvm_assigned_dev_kernel *assigned_dev)
217{ 216{
218 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier); 217 kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
219 assigned_dev->ack_notifier.gsi = -1; 218 assigned_dev->ack_notifier.gsi = -1;
220 219
221 if (assigned_dev->irq_source_id != -1) 220 if (assigned_dev->irq_source_id != -1)
@@ -417,6 +416,7 @@ static int assigned_device_enable_guest_msi(struct kvm *kvm,
417{ 416{
418 dev->guest_irq = irq->guest_irq; 417 dev->guest_irq = irq->guest_irq;
419 dev->ack_notifier.gsi = -1; 418 dev->ack_notifier.gsi = -1;
419 dev->host_irq_disabled = false;
420 return 0; 420 return 0;
421} 421}
422#endif 422#endif
@@ -427,6 +427,7 @@ static int assigned_device_enable_guest_msix(struct kvm *kvm,
427{ 427{
428 dev->guest_irq = irq->guest_irq; 428 dev->guest_irq = irq->guest_irq;
429 dev->ack_notifier.gsi = -1; 429 dev->ack_notifier.gsi = -1;
430 dev->host_irq_disabled = false;
430 return 0; 431 return 0;
431} 432}
432#endif 433#endif
@@ -693,11 +694,6 @@ out:
693} 694}
694#endif 695#endif
695 696
696static inline int valid_vcpu(int n)
697{
698 return likely(n >= 0 && n < KVM_MAX_VCPUS);
699}
700
701inline int kvm_is_mmio_pfn(pfn_t pfn) 697inline int kvm_is_mmio_pfn(pfn_t pfn)
702{ 698{
703 if (pfn_valid(pfn)) { 699 if (pfn_valid(pfn)) {
@@ -745,12 +741,9 @@ static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
745 if (alloc_cpumask_var(&cpus, GFP_ATOMIC)) 741 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
746 cpumask_clear(cpus); 742 cpumask_clear(cpus);
747 743
748 me = get_cpu();
749 spin_lock(&kvm->requests_lock); 744 spin_lock(&kvm->requests_lock);
750 for (i = 0; i < KVM_MAX_VCPUS; ++i) { 745 me = smp_processor_id();
751 vcpu = kvm->vcpus[i]; 746 kvm_for_each_vcpu(i, vcpu, kvm) {
752 if (!vcpu)
753 continue;
754 if (test_and_set_bit(req, &vcpu->requests)) 747 if (test_and_set_bit(req, &vcpu->requests))
755 continue; 748 continue;
756 cpu = vcpu->cpu; 749 cpu = vcpu->cpu;
@@ -764,7 +757,6 @@ static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
764 else 757 else
765 called = false; 758 called = false;
766 spin_unlock(&kvm->requests_lock); 759 spin_unlock(&kvm->requests_lock);
767 put_cpu();
768 free_cpumask_var(cpus); 760 free_cpumask_var(cpus);
769 return called; 761 return called;
770} 762}
@@ -986,7 +978,9 @@ static struct kvm *kvm_create_vm(void)
986 spin_lock_init(&kvm->mmu_lock); 978 spin_lock_init(&kvm->mmu_lock);
987 spin_lock_init(&kvm->requests_lock); 979 spin_lock_init(&kvm->requests_lock);
988 kvm_io_bus_init(&kvm->pio_bus); 980 kvm_io_bus_init(&kvm->pio_bus);
981 kvm_eventfd_init(kvm);
989 mutex_init(&kvm->lock); 982 mutex_init(&kvm->lock);
983 mutex_init(&kvm->irq_lock);
990 kvm_io_bus_init(&kvm->mmio_bus); 984 kvm_io_bus_init(&kvm->mmio_bus);
991 init_rwsem(&kvm->slots_lock); 985 init_rwsem(&kvm->slots_lock);
992 atomic_set(&kvm->users_count, 1); 986 atomic_set(&kvm->users_count, 1);
@@ -1006,19 +1000,25 @@ out:
1006static void kvm_free_physmem_slot(struct kvm_memory_slot *free, 1000static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
1007 struct kvm_memory_slot *dont) 1001 struct kvm_memory_slot *dont)
1008{ 1002{
1003 int i;
1004
1009 if (!dont || free->rmap != dont->rmap) 1005 if (!dont || free->rmap != dont->rmap)
1010 vfree(free->rmap); 1006 vfree(free->rmap);
1011 1007
1012 if (!dont || free->dirty_bitmap != dont->dirty_bitmap) 1008 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
1013 vfree(free->dirty_bitmap); 1009 vfree(free->dirty_bitmap);
1014 1010
1015 if (!dont || free->lpage_info != dont->lpage_info) 1011
1016 vfree(free->lpage_info); 1012 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
1013 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
1014 vfree(free->lpage_info[i]);
1015 free->lpage_info[i] = NULL;
1016 }
1017 }
1017 1018
1018 free->npages = 0; 1019 free->npages = 0;
1019 free->dirty_bitmap = NULL; 1020 free->dirty_bitmap = NULL;
1020 free->rmap = NULL; 1021 free->rmap = NULL;
1021 free->lpage_info = NULL;
1022} 1022}
1023 1023
1024void kvm_free_physmem(struct kvm *kvm) 1024void kvm_free_physmem(struct kvm *kvm)
@@ -1071,6 +1071,8 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)
1071{ 1071{
1072 struct kvm *kvm = filp->private_data; 1072 struct kvm *kvm = filp->private_data;
1073 1073
1074 kvm_irqfd_release(kvm);
1075
1074 kvm_put_kvm(kvm); 1076 kvm_put_kvm(kvm);
1075 return 0; 1077 return 0;
1076} 1078}
@@ -1089,8 +1091,8 @@ int __kvm_set_memory_region(struct kvm *kvm,
1089{ 1091{
1090 int r; 1092 int r;
1091 gfn_t base_gfn; 1093 gfn_t base_gfn;
1092 unsigned long npages, ugfn; 1094 unsigned long npages;
1093 unsigned long largepages, i; 1095 unsigned long i;
1094 struct kvm_memory_slot *memslot; 1096 struct kvm_memory_slot *memslot;
1095 struct kvm_memory_slot old, new; 1097 struct kvm_memory_slot old, new;
1096 1098
@@ -1164,31 +1166,51 @@ int __kvm_set_memory_region(struct kvm *kvm,
1164 else 1166 else
1165 new.userspace_addr = 0; 1167 new.userspace_addr = 0;
1166 } 1168 }
1167 if (npages && !new.lpage_info) { 1169 if (!npages)
1168 largepages = 1 + (base_gfn + npages - 1) / KVM_PAGES_PER_HPAGE; 1170 goto skip_lpage;
1169 largepages -= base_gfn / KVM_PAGES_PER_HPAGE;
1170 1171
1171 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info)); 1172 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
1173 unsigned long ugfn;
1174 unsigned long j;
1175 int lpages;
1176 int level = i + 2;
1172 1177
1173 if (!new.lpage_info) 1178 /* Avoid unused variable warning if no large pages */
1179 (void)level;
1180
1181 if (new.lpage_info[i])
1182 continue;
1183
1184 lpages = 1 + (base_gfn + npages - 1) /
1185 KVM_PAGES_PER_HPAGE(level);
1186 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
1187
1188 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
1189
1190 if (!new.lpage_info[i])
1174 goto out_free; 1191 goto out_free;
1175 1192
1176 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info)); 1193 memset(new.lpage_info[i], 0,
1194 lpages * sizeof(*new.lpage_info[i]));
1177 1195
1178 if (base_gfn % KVM_PAGES_PER_HPAGE) 1196 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
1179 new.lpage_info[0].write_count = 1; 1197 new.lpage_info[i][0].write_count = 1;
1180 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE) 1198 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
1181 new.lpage_info[largepages-1].write_count = 1; 1199 new.lpage_info[i][lpages - 1].write_count = 1;
1182 ugfn = new.userspace_addr >> PAGE_SHIFT; 1200 ugfn = new.userspace_addr >> PAGE_SHIFT;
1183 /* 1201 /*
1184 * If the gfn and userspace address are not aligned wrt each 1202 * If the gfn and userspace address are not aligned wrt each
1185 * other, disable large page support for this slot 1203 * other, or if explicitly asked to, disable large page
1204 * support for this slot
1186 */ 1205 */
1187 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE - 1)) 1206 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
1188 for (i = 0; i < largepages; ++i) 1207 !largepages_enabled)
1189 new.lpage_info[i].write_count = 1; 1208 for (j = 0; j < lpages; ++j)
1209 new.lpage_info[i][j].write_count = 1;
1190 } 1210 }
1191 1211
1212skip_lpage:
1213
1192 /* Allocate page dirty bitmap if needed */ 1214 /* Allocate page dirty bitmap if needed */
1193 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { 1215 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1194 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; 1216 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
@@ -1200,6 +1222,10 @@ int __kvm_set_memory_region(struct kvm *kvm,
1200 if (old.npages) 1222 if (old.npages)
1201 kvm_arch_flush_shadow(kvm); 1223 kvm_arch_flush_shadow(kvm);
1202 } 1224 }
1225#else /* not defined CONFIG_S390 */
1226 new.user_alloc = user_alloc;
1227 if (user_alloc)
1228 new.userspace_addr = mem->userspace_addr;
1203#endif /* not defined CONFIG_S390 */ 1229#endif /* not defined CONFIG_S390 */
1204 1230
1205 if (!npages) 1231 if (!npages)
@@ -1299,6 +1325,12 @@ out:
1299 return r; 1325 return r;
1300} 1326}
1301 1327
1328void kvm_disable_largepages(void)
1329{
1330 largepages_enabled = false;
1331}
1332EXPORT_SYMBOL_GPL(kvm_disable_largepages);
1333
1302int is_error_page(struct page *page) 1334int is_error_page(struct page *page)
1303{ 1335{
1304 return page == bad_page; 1336 return page == bad_page;
@@ -1635,9 +1667,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1635 for (;;) { 1667 for (;;) {
1636 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE); 1668 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1637 1669
1638 if ((kvm_arch_interrupt_allowed(vcpu) && 1670 if (kvm_arch_vcpu_runnable(vcpu)) {
1639 kvm_cpu_has_interrupt(vcpu)) ||
1640 kvm_arch_vcpu_runnable(vcpu)) {
1641 set_bit(KVM_REQ_UNHALT, &vcpu->requests); 1671 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1642 break; 1672 break;
1643 } 1673 }
@@ -1714,24 +1744,18 @@ static struct file_operations kvm_vcpu_fops = {
1714 */ 1744 */
1715static int create_vcpu_fd(struct kvm_vcpu *vcpu) 1745static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1716{ 1746{
1717 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0); 1747 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1718 if (fd < 0)
1719 kvm_put_kvm(vcpu->kvm);
1720 return fd;
1721} 1748}
1722 1749
1723/* 1750/*
1724 * Creates some virtual cpus. Good luck creating more than one. 1751 * Creates some virtual cpus. Good luck creating more than one.
1725 */ 1752 */
1726static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) 1753static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
1727{ 1754{
1728 int r; 1755 int r;
1729 struct kvm_vcpu *vcpu; 1756 struct kvm_vcpu *vcpu, *v;
1730
1731 if (!valid_vcpu(n))
1732 return -EINVAL;
1733 1757
1734 vcpu = kvm_arch_vcpu_create(kvm, n); 1758 vcpu = kvm_arch_vcpu_create(kvm, id);
1735 if (IS_ERR(vcpu)) 1759 if (IS_ERR(vcpu))
1736 return PTR_ERR(vcpu); 1760 return PTR_ERR(vcpu);
1737 1761
@@ -1742,23 +1766,38 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1742 return r; 1766 return r;
1743 1767
1744 mutex_lock(&kvm->lock); 1768 mutex_lock(&kvm->lock);
1745 if (kvm->vcpus[n]) { 1769 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1746 r = -EEXIST; 1770 r = -EINVAL;
1747 goto vcpu_destroy; 1771 goto vcpu_destroy;
1748 } 1772 }
1749 kvm->vcpus[n] = vcpu; 1773
1750 mutex_unlock(&kvm->lock); 1774 kvm_for_each_vcpu(r, v, kvm)
1775 if (v->vcpu_id == id) {
1776 r = -EEXIST;
1777 goto vcpu_destroy;
1778 }
1779
1780 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
1751 1781
1752 /* Now it's all set up, let userspace reach it */ 1782 /* Now it's all set up, let userspace reach it */
1753 kvm_get_kvm(kvm); 1783 kvm_get_kvm(kvm);
1754 r = create_vcpu_fd(vcpu); 1784 r = create_vcpu_fd(vcpu);
1755 if (r < 0) 1785 if (r < 0) {
1756 goto unlink; 1786 kvm_put_kvm(kvm);
1787 goto vcpu_destroy;
1788 }
1789
1790 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1791 smp_wmb();
1792 atomic_inc(&kvm->online_vcpus);
1793
1794#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1795 if (kvm->bsp_vcpu_id == id)
1796 kvm->bsp_vcpu = vcpu;
1797#endif
1798 mutex_unlock(&kvm->lock);
1757 return r; 1799 return r;
1758 1800
1759unlink:
1760 mutex_lock(&kvm->lock);
1761 kvm->vcpus[n] = NULL;
1762vcpu_destroy: 1801vcpu_destroy:
1763 mutex_unlock(&kvm->lock); 1802 mutex_unlock(&kvm->lock);
1764 kvm_arch_vcpu_destroy(vcpu); 1803 kvm_arch_vcpu_destroy(vcpu);
@@ -2199,6 +2238,7 @@ static long kvm_vm_ioctl(struct file *filp,
2199 vfree(entries); 2238 vfree(entries);
2200 break; 2239 break;
2201 } 2240 }
2241#endif /* KVM_CAP_IRQ_ROUTING */
2202#ifdef __KVM_HAVE_MSIX 2242#ifdef __KVM_HAVE_MSIX
2203 case KVM_ASSIGN_SET_MSIX_NR: { 2243 case KVM_ASSIGN_SET_MSIX_NR: {
2204 struct kvm_assigned_msix_nr entry_nr; 2244 struct kvm_assigned_msix_nr entry_nr;
@@ -2221,7 +2261,35 @@ static long kvm_vm_ioctl(struct file *filp,
2221 break; 2261 break;
2222 } 2262 }
2223#endif 2263#endif
2224#endif /* KVM_CAP_IRQ_ROUTING */ 2264 case KVM_IRQFD: {
2265 struct kvm_irqfd data;
2266
2267 r = -EFAULT;
2268 if (copy_from_user(&data, argp, sizeof data))
2269 goto out;
2270 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2271 break;
2272 }
2273 case KVM_IOEVENTFD: {
2274 struct kvm_ioeventfd data;
2275
2276 r = -EFAULT;
2277 if (copy_from_user(&data, argp, sizeof data))
2278 goto out;
2279 r = kvm_ioeventfd(kvm, &data);
2280 break;
2281 }
2282#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2283 case KVM_SET_BOOT_CPU_ID:
2284 r = 0;
2285 mutex_lock(&kvm->lock);
2286 if (atomic_read(&kvm->online_vcpus) != 0)
2287 r = -EBUSY;
2288 else
2289 kvm->bsp_vcpu_id = arg;
2290 mutex_unlock(&kvm->lock);
2291 break;
2292#endif
2225 default: 2293 default:
2226 r = kvm_arch_vm_ioctl(filp, ioctl, arg); 2294 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
2227 } 2295 }
@@ -2288,6 +2356,9 @@ static long kvm_dev_ioctl_check_extension_generic(long arg)
2288 case KVM_CAP_USER_MEMORY: 2356 case KVM_CAP_USER_MEMORY:
2289 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: 2357 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2290 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS: 2358 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
2359#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2360 case KVM_CAP_SET_BOOT_CPU_ID:
2361#endif
2291 return 1; 2362 return 1;
2292#ifdef CONFIG_HAVE_KVM_IRQCHIP 2363#ifdef CONFIG_HAVE_KVM_IRQCHIP
2293 case KVM_CAP_IRQ_ROUTING: 2364 case KVM_CAP_IRQ_ROUTING:
@@ -2335,7 +2406,7 @@ static long kvm_dev_ioctl(struct file *filp,
2335 case KVM_TRACE_ENABLE: 2406 case KVM_TRACE_ENABLE:
2336 case KVM_TRACE_PAUSE: 2407 case KVM_TRACE_PAUSE:
2337 case KVM_TRACE_DISABLE: 2408 case KVM_TRACE_DISABLE:
2338 r = kvm_trace_ioctl(ioctl, arg); 2409 r = -EOPNOTSUPP;
2339 break; 2410 break;
2340 default: 2411 default:
2341 return kvm_arch_dev_ioctl(filp, ioctl, arg); 2412 return kvm_arch_dev_ioctl(filp, ioctl, arg);
@@ -2449,26 +2520,71 @@ void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2449 } 2520 }
2450} 2521}
2451 2522
2452struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, 2523/* kvm_io_bus_write - called under kvm->slots_lock */
2453 gpa_t addr, int len, int is_write) 2524int kvm_io_bus_write(struct kvm_io_bus *bus, gpa_t addr,
2525 int len, const void *val)
2454{ 2526{
2455 int i; 2527 int i;
2528 for (i = 0; i < bus->dev_count; i++)
2529 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2530 return 0;
2531 return -EOPNOTSUPP;
2532}
2456 2533
2457 for (i = 0; i < bus->dev_count; i++) { 2534/* kvm_io_bus_read - called under kvm->slots_lock */
2458 struct kvm_io_device *pos = bus->devs[i]; 2535int kvm_io_bus_read(struct kvm_io_bus *bus, gpa_t addr, int len, void *val)
2536{
2537 int i;
2538 for (i = 0; i < bus->dev_count; i++)
2539 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2540 return 0;
2541 return -EOPNOTSUPP;
2542}
2459 2543
2460 if (pos->in_range(pos, addr, len, is_write)) 2544int kvm_io_bus_register_dev(struct kvm *kvm, struct kvm_io_bus *bus,
2461 return pos; 2545 struct kvm_io_device *dev)
2462 } 2546{
2547 int ret;
2463 2548
2464 return NULL; 2549 down_write(&kvm->slots_lock);
2550 ret = __kvm_io_bus_register_dev(bus, dev);
2551 up_write(&kvm->slots_lock);
2552
2553 return ret;
2465} 2554}
2466 2555
2467void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) 2556/* An unlocked version. Caller must have write lock on slots_lock. */
2557int __kvm_io_bus_register_dev(struct kvm_io_bus *bus,
2558 struct kvm_io_device *dev)
2468{ 2559{
2469 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); 2560 if (bus->dev_count > NR_IOBUS_DEVS-1)
2561 return -ENOSPC;
2470 2562
2471 bus->devs[bus->dev_count++] = dev; 2563 bus->devs[bus->dev_count++] = dev;
2564
2565 return 0;
2566}
2567
2568void kvm_io_bus_unregister_dev(struct kvm *kvm,
2569 struct kvm_io_bus *bus,
2570 struct kvm_io_device *dev)
2571{
2572 down_write(&kvm->slots_lock);
2573 __kvm_io_bus_unregister_dev(bus, dev);
2574 up_write(&kvm->slots_lock);
2575}
2576
2577/* An unlocked version. Caller must have write lock on slots_lock. */
2578void __kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
2579 struct kvm_io_device *dev)
2580{
2581 int i;
2582
2583 for (i = 0; i < bus->dev_count; i++)
2584 if (bus->devs[i] == dev) {
2585 bus->devs[i] = bus->devs[--bus->dev_count];
2586 break;
2587 }
2472} 2588}
2473 2589
2474static struct notifier_block kvm_cpu_notifier = { 2590static struct notifier_block kvm_cpu_notifier = {
@@ -2501,11 +2617,9 @@ static int vcpu_stat_get(void *_offset, u64 *val)
2501 *val = 0; 2617 *val = 0;
2502 spin_lock(&kvm_lock); 2618 spin_lock(&kvm_lock);
2503 list_for_each_entry(kvm, &vm_list, vm_list) 2619 list_for_each_entry(kvm, &vm_list, vm_list)
2504 for (i = 0; i < KVM_MAX_VCPUS; ++i) { 2620 kvm_for_each_vcpu(i, vcpu, kvm)
2505 vcpu = kvm->vcpus[i]; 2621 *val += *(u32 *)((void *)vcpu + offset);
2506 if (vcpu) 2622
2507 *val += *(u32 *)((void *)vcpu + offset);
2508 }
2509 spin_unlock(&kvm_lock); 2623 spin_unlock(&kvm_lock);
2510 return 0; 2624 return 0;
2511} 2625}
@@ -2679,15 +2793,15 @@ out_free_0:
2679 __free_page(bad_page); 2793 __free_page(bad_page);
2680out: 2794out:
2681 kvm_arch_exit(); 2795 kvm_arch_exit();
2682 kvm_exit_debug();
2683out_fail: 2796out_fail:
2797 kvm_exit_debug();
2684 return r; 2798 return r;
2685} 2799}
2686EXPORT_SYMBOL_GPL(kvm_init); 2800EXPORT_SYMBOL_GPL(kvm_init);
2687 2801
2688void kvm_exit(void) 2802void kvm_exit(void)
2689{ 2803{
2690 kvm_trace_cleanup(); 2804 tracepoint_synchronize_unregister();
2691 misc_deregister(&kvm_dev); 2805 misc_deregister(&kvm_dev);
2692 kmem_cache_destroy(kvm_vcpu_cache); 2806 kmem_cache_destroy(kvm_vcpu_cache);
2693 sysdev_unregister(&kvm_sysdev); 2807 sysdev_unregister(&kvm_sysdev);