aboutsummaryrefslogtreecommitdiffstats
path: root/virt/kvm
diff options
context:
space:
mode:
Diffstat (limited to 'virt/kvm')
-rw-r--r--virt/kvm/ioapic.c30
-rw-r--r--virt/kvm/ioapic.h2
-rw-r--r--virt/kvm/iommu.c113
3 files changed, 107 insertions, 38 deletions
diff --git a/virt/kvm/ioapic.c b/virt/kvm/ioapic.c
index 03a5eb22da2b..7c79c1d76d0c 100644
--- a/virt/kvm/ioapic.c
+++ b/virt/kvm/ioapic.c
@@ -197,7 +197,7 @@ int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
197 union kvm_ioapic_redirect_entry entry; 197 union kvm_ioapic_redirect_entry entry;
198 int ret = 1; 198 int ret = 1;
199 199
200 mutex_lock(&ioapic->lock); 200 spin_lock(&ioapic->lock);
201 if (irq >= 0 && irq < IOAPIC_NUM_PINS) { 201 if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
202 entry = ioapic->redirtbl[irq]; 202 entry = ioapic->redirtbl[irq];
203 level ^= entry.fields.polarity; 203 level ^= entry.fields.polarity;
@@ -214,7 +214,7 @@ int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
214 } 214 }
215 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0); 215 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
216 } 216 }
217 mutex_unlock(&ioapic->lock); 217 spin_unlock(&ioapic->lock);
218 218
219 return ret; 219 return ret;
220} 220}
@@ -238,9 +238,9 @@ static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector,
238 * is dropped it will be put into irr and will be delivered 238 * is dropped it will be put into irr and will be delivered
239 * after ack notifier returns. 239 * after ack notifier returns.
240 */ 240 */
241 mutex_unlock(&ioapic->lock); 241 spin_unlock(&ioapic->lock);
242 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i); 242 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
243 mutex_lock(&ioapic->lock); 243 spin_lock(&ioapic->lock);
244 244
245 if (trigger_mode != IOAPIC_LEVEL_TRIG) 245 if (trigger_mode != IOAPIC_LEVEL_TRIG)
246 continue; 246 continue;
@@ -259,9 +259,9 @@ void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
259 smp_rmb(); 259 smp_rmb();
260 if (!test_bit(vector, ioapic->handled_vectors)) 260 if (!test_bit(vector, ioapic->handled_vectors))
261 return; 261 return;
262 mutex_lock(&ioapic->lock); 262 spin_lock(&ioapic->lock);
263 __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode); 263 __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode);
264 mutex_unlock(&ioapic->lock); 264 spin_unlock(&ioapic->lock);
265} 265}
266 266
267static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev) 267static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
@@ -287,7 +287,7 @@ static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
287 ASSERT(!(addr & 0xf)); /* check alignment */ 287 ASSERT(!(addr & 0xf)); /* check alignment */
288 288
289 addr &= 0xff; 289 addr &= 0xff;
290 mutex_lock(&ioapic->lock); 290 spin_lock(&ioapic->lock);
291 switch (addr) { 291 switch (addr) {
292 case IOAPIC_REG_SELECT: 292 case IOAPIC_REG_SELECT:
293 result = ioapic->ioregsel; 293 result = ioapic->ioregsel;
@@ -301,7 +301,7 @@ static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
301 result = 0; 301 result = 0;
302 break; 302 break;
303 } 303 }
304 mutex_unlock(&ioapic->lock); 304 spin_unlock(&ioapic->lock);
305 305
306 switch (len) { 306 switch (len) {
307 case 8: 307 case 8:
@@ -338,7 +338,7 @@ static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
338 } 338 }
339 339
340 addr &= 0xff; 340 addr &= 0xff;
341 mutex_lock(&ioapic->lock); 341 spin_lock(&ioapic->lock);
342 switch (addr) { 342 switch (addr) {
343 case IOAPIC_REG_SELECT: 343 case IOAPIC_REG_SELECT:
344 ioapic->ioregsel = data; 344 ioapic->ioregsel = data;
@@ -356,7 +356,7 @@ static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
356 default: 356 default:
357 break; 357 break;
358 } 358 }
359 mutex_unlock(&ioapic->lock); 359 spin_unlock(&ioapic->lock);
360 return 0; 360 return 0;
361} 361}
362 362
@@ -386,7 +386,7 @@ int kvm_ioapic_init(struct kvm *kvm)
386 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL); 386 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
387 if (!ioapic) 387 if (!ioapic)
388 return -ENOMEM; 388 return -ENOMEM;
389 mutex_init(&ioapic->lock); 389 spin_lock_init(&ioapic->lock);
390 kvm->arch.vioapic = ioapic; 390 kvm->arch.vioapic = ioapic;
391 kvm_ioapic_reset(ioapic); 391 kvm_ioapic_reset(ioapic);
392 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops); 392 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
@@ -419,9 +419,9 @@ int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
419 if (!ioapic) 419 if (!ioapic)
420 return -EINVAL; 420 return -EINVAL;
421 421
422 mutex_lock(&ioapic->lock); 422 spin_lock(&ioapic->lock);
423 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state)); 423 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
424 mutex_unlock(&ioapic->lock); 424 spin_unlock(&ioapic->lock);
425 return 0; 425 return 0;
426} 426}
427 427
@@ -431,9 +431,9 @@ int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
431 if (!ioapic) 431 if (!ioapic)
432 return -EINVAL; 432 return -EINVAL;
433 433
434 mutex_lock(&ioapic->lock); 434 spin_lock(&ioapic->lock);
435 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state)); 435 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
436 update_handled_vectors(ioapic); 436 update_handled_vectors(ioapic);
437 mutex_unlock(&ioapic->lock); 437 spin_unlock(&ioapic->lock);
438 return 0; 438 return 0;
439} 439}
diff --git a/virt/kvm/ioapic.h b/virt/kvm/ioapic.h
index 8a751b78a430..0b190c34ccc3 100644
--- a/virt/kvm/ioapic.h
+++ b/virt/kvm/ioapic.h
@@ -45,7 +45,7 @@ struct kvm_ioapic {
45 struct kvm_io_device dev; 45 struct kvm_io_device dev;
46 struct kvm *kvm; 46 struct kvm *kvm;
47 void (*ack_notifier)(void *opaque, int irq); 47 void (*ack_notifier)(void *opaque, int irq);
48 struct mutex lock; 48 spinlock_t lock;
49 DECLARE_BITMAP(handled_vectors, 256); 49 DECLARE_BITMAP(handled_vectors, 256);
50}; 50};
51 51
diff --git a/virt/kvm/iommu.c b/virt/kvm/iommu.c
index 80fd3ad3b2de..11692b9e8830 100644
--- a/virt/kvm/iommu.c
+++ b/virt/kvm/iommu.c
@@ -32,12 +32,30 @@ static int kvm_iommu_unmap_memslots(struct kvm *kvm);
32static void kvm_iommu_put_pages(struct kvm *kvm, 32static void kvm_iommu_put_pages(struct kvm *kvm,
33 gfn_t base_gfn, unsigned long npages); 33 gfn_t base_gfn, unsigned long npages);
34 34
35static pfn_t kvm_pin_pages(struct kvm *kvm, struct kvm_memory_slot *slot,
36 gfn_t gfn, unsigned long size)
37{
38 gfn_t end_gfn;
39 pfn_t pfn;
40
41 pfn = gfn_to_pfn_memslot(kvm, slot, gfn);
42 end_gfn = gfn + (size >> PAGE_SHIFT);
43 gfn += 1;
44
45 if (is_error_pfn(pfn))
46 return pfn;
47
48 while (gfn < end_gfn)
49 gfn_to_pfn_memslot(kvm, slot, gfn++);
50
51 return pfn;
52}
53
35int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot) 54int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
36{ 55{
37 gfn_t gfn = slot->base_gfn; 56 gfn_t gfn, end_gfn;
38 unsigned long npages = slot->npages;
39 pfn_t pfn; 57 pfn_t pfn;
40 int i, r = 0; 58 int r = 0;
41 struct iommu_domain *domain = kvm->arch.iommu_domain; 59 struct iommu_domain *domain = kvm->arch.iommu_domain;
42 int flags; 60 int flags;
43 61
@@ -45,31 +63,62 @@ int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
45 if (!domain) 63 if (!domain)
46 return 0; 64 return 0;
47 65
66 gfn = slot->base_gfn;
67 end_gfn = gfn + slot->npages;
68
48 flags = IOMMU_READ | IOMMU_WRITE; 69 flags = IOMMU_READ | IOMMU_WRITE;
49 if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY) 70 if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)
50 flags |= IOMMU_CACHE; 71 flags |= IOMMU_CACHE;
51 72
52 for (i = 0; i < npages; i++) { 73
53 /* check if already mapped */ 74 while (gfn < end_gfn) {
54 if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn))) 75 unsigned long page_size;
76
77 /* Check if already mapped */
78 if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn))) {
79 gfn += 1;
80 continue;
81 }
82
83 /* Get the page size we could use to map */
84 page_size = kvm_host_page_size(kvm, gfn);
85
86 /* Make sure the page_size does not exceed the memslot */
87 while ((gfn + (page_size >> PAGE_SHIFT)) > end_gfn)
88 page_size >>= 1;
89
90 /* Make sure gfn is aligned to the page size we want to map */
91 while ((gfn << PAGE_SHIFT) & (page_size - 1))
92 page_size >>= 1;
93
94 /*
95 * Pin all pages we are about to map in memory. This is
96 * important because we unmap and unpin in 4kb steps later.
97 */
98 pfn = kvm_pin_pages(kvm, slot, gfn, page_size);
99 if (is_error_pfn(pfn)) {
100 gfn += 1;
55 continue; 101 continue;
102 }
56 103
57 pfn = gfn_to_pfn_memslot(kvm, slot, gfn); 104 /* Map into IO address space */
58 r = iommu_map_range(domain, 105 r = iommu_map(domain, gfn_to_gpa(gfn), pfn_to_hpa(pfn),
59 gfn_to_gpa(gfn), 106 get_order(page_size), flags);
60 pfn_to_hpa(pfn),
61 PAGE_SIZE, flags);
62 if (r) { 107 if (r) {
63 printk(KERN_ERR "kvm_iommu_map_address:" 108 printk(KERN_ERR "kvm_iommu_map_address:"
64 "iommu failed to map pfn=%lx\n", pfn); 109 "iommu failed to map pfn=%lx\n", pfn);
65 goto unmap_pages; 110 goto unmap_pages;
66 } 111 }
67 gfn++; 112
113 gfn += page_size >> PAGE_SHIFT;
114
115
68 } 116 }
117
69 return 0; 118 return 0;
70 119
71unmap_pages: 120unmap_pages:
72 kvm_iommu_put_pages(kvm, slot->base_gfn, i); 121 kvm_iommu_put_pages(kvm, slot->base_gfn, gfn);
73 return r; 122 return r;
74} 123}
75 124
@@ -189,27 +238,47 @@ out_unmap:
189 return r; 238 return r;
190} 239}
191 240
241static void kvm_unpin_pages(struct kvm *kvm, pfn_t pfn, unsigned long npages)
242{
243 unsigned long i;
244
245 for (i = 0; i < npages; ++i)
246 kvm_release_pfn_clean(pfn + i);
247}
248
192static void kvm_iommu_put_pages(struct kvm *kvm, 249static void kvm_iommu_put_pages(struct kvm *kvm,
193 gfn_t base_gfn, unsigned long npages) 250 gfn_t base_gfn, unsigned long npages)
194{ 251{
195 gfn_t gfn = base_gfn; 252 struct iommu_domain *domain;
253 gfn_t end_gfn, gfn;
196 pfn_t pfn; 254 pfn_t pfn;
197 struct iommu_domain *domain = kvm->arch.iommu_domain;
198 unsigned long i;
199 u64 phys; 255 u64 phys;
200 256
257 domain = kvm->arch.iommu_domain;
258 end_gfn = base_gfn + npages;
259 gfn = base_gfn;
260
201 /* check if iommu exists and in use */ 261 /* check if iommu exists and in use */
202 if (!domain) 262 if (!domain)
203 return; 263 return;
204 264
205 for (i = 0; i < npages; i++) { 265 while (gfn < end_gfn) {
266 unsigned long unmap_pages;
267 int order;
268
269 /* Get physical address */
206 phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn)); 270 phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
207 pfn = phys >> PAGE_SHIFT; 271 pfn = phys >> PAGE_SHIFT;
208 kvm_release_pfn_clean(pfn); 272
209 gfn++; 273 /* Unmap address from IO address space */
210 } 274 order = iommu_unmap(domain, gfn_to_gpa(gfn), PAGE_SIZE);
275 unmap_pages = 1ULL << order;
211 276
212 iommu_unmap_range(domain, gfn_to_gpa(base_gfn), PAGE_SIZE * npages); 277 /* Unpin all pages we just unmapped to not leak any memory */
278 kvm_unpin_pages(kvm, pfn, unmap_pages);
279
280 gfn += unmap_pages;
281 }
213} 282}
214 283
215static int kvm_iommu_unmap_memslots(struct kvm *kvm) 284static int kvm_iommu_unmap_memslots(struct kvm *kvm)