]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - virt/kvm/arm/vgic/vgic.c
KVM: arm/arm64: Properly protect VGIC locks from IRQs
[mirror_ubuntu-bionic-kernel.git] / virt / kvm / arm / vgic / vgic.c
1 /*
2 * Copyright (C) 2015, 2016 ARM Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include <linux/kvm.h>
18 #include <linux/kvm_host.h>
19 #include <linux/list_sort.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22
23 #include "vgic.h"
24
25 #define CREATE_TRACE_POINTS
26 #include "trace.h"
27
28 #ifdef CONFIG_DEBUG_SPINLOCK
29 #define DEBUG_SPINLOCK_BUG_ON(p) BUG_ON(p)
30 #else
31 #define DEBUG_SPINLOCK_BUG_ON(p)
32 #endif
33
34 struct vgic_global kvm_vgic_global_state __ro_after_init = {
35 .gicv3_cpuif = STATIC_KEY_FALSE_INIT,
36 };
37
38 /*
39 * Locking order is always:
40 * kvm->lock (mutex)
41 * its->cmd_lock (mutex)
42 * its->its_lock (mutex)
43 * vgic_cpu->ap_list_lock must be taken with IRQs disabled
44 * kvm->lpi_list_lock must be taken with IRQs disabled
45 * vgic_irq->irq_lock must be taken with IRQs disabled
46 *
47 * As the ap_list_lock might be taken from the timer interrupt handler,
48 * we have to disable IRQs before taking this lock and everything lower
49 * than it.
50 *
51 * If you need to take multiple locks, always take the upper lock first,
52 * then the lower ones, e.g. first take the its_lock, then the irq_lock.
53 * If you are already holding a lock and need to take a higher one, you
54 * have to drop the lower ranking lock first and re-aquire it after having
55 * taken the upper one.
56 *
57 * When taking more than one ap_list_lock at the same time, always take the
58 * lowest numbered VCPU's ap_list_lock first, so:
59 * vcpuX->vcpu_id < vcpuY->vcpu_id:
60 * spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
61 * spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
62 *
63 * Since the VGIC must support injecting virtual interrupts from ISRs, we have
64 * to use the spin_lock_irqsave/spin_unlock_irqrestore versions of outer
65 * spinlocks for any lock that may be taken while injecting an interrupt.
66 */
67
68 /*
69 * Iterate over the VM's list of mapped LPIs to find the one with a
70 * matching interrupt ID and return a reference to the IRQ structure.
71 */
72 static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)
73 {
74 struct vgic_dist *dist = &kvm->arch.vgic;
75 struct vgic_irq *irq = NULL;
76 unsigned long flags;
77
78 spin_lock_irqsave(&dist->lpi_list_lock, flags);
79
80 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
81 if (irq->intid != intid)
82 continue;
83
84 /*
85 * This increases the refcount, the caller is expected to
86 * call vgic_put_irq() later once it's finished with the IRQ.
87 */
88 vgic_get_irq_kref(irq);
89 goto out_unlock;
90 }
91 irq = NULL;
92
93 out_unlock:
94 spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
95
96 return irq;
97 }
98
99 /*
100 * This looks up the virtual interrupt ID to get the corresponding
101 * struct vgic_irq. It also increases the refcount, so any caller is expected
102 * to call vgic_put_irq() once it's finished with this IRQ.
103 */
104 struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
105 u32 intid)
106 {
107 /* SGIs and PPIs */
108 if (intid <= VGIC_MAX_PRIVATE)
109 return &vcpu->arch.vgic_cpu.private_irqs[intid];
110
111 /* SPIs */
112 if (intid <= VGIC_MAX_SPI)
113 return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
114
115 /* LPIs */
116 if (intid >= VGIC_MIN_LPI)
117 return vgic_get_lpi(kvm, intid);
118
119 WARN(1, "Looking up struct vgic_irq for reserved INTID");
120 return NULL;
121 }
122
123 /*
124 * We can't do anything in here, because we lack the kvm pointer to
125 * lock and remove the item from the lpi_list. So we keep this function
126 * empty and use the return value of kref_put() to trigger the freeing.
127 */
128 static void vgic_irq_release(struct kref *ref)
129 {
130 }
131
132 void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
133 {
134 struct vgic_dist *dist = &kvm->arch.vgic;
135 unsigned long flags;
136
137 if (irq->intid < VGIC_MIN_LPI)
138 return;
139
140 spin_lock_irqsave(&dist->lpi_list_lock, flags);
141 if (!kref_put(&irq->refcount, vgic_irq_release)) {
142 spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
143 return;
144 };
145
146 list_del(&irq->lpi_list);
147 dist->lpi_list_count--;
148 spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
149
150 kfree(irq);
151 }
152
153 /**
154 * kvm_vgic_target_oracle - compute the target vcpu for an irq
155 *
156 * @irq: The irq to route. Must be already locked.
157 *
158 * Based on the current state of the interrupt (enabled, pending,
159 * active, vcpu and target_vcpu), compute the next vcpu this should be
160 * given to. Return NULL if this shouldn't be injected at all.
161 *
162 * Requires the IRQ lock to be held.
163 */
164 static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq)
165 {
166 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
167
168 /* If the interrupt is active, it must stay on the current vcpu */
169 if (irq->active)
170 return irq->vcpu ? : irq->target_vcpu;
171
172 /*
173 * If the IRQ is not active but enabled and pending, we should direct
174 * it to its configured target VCPU.
175 * If the distributor is disabled, pending interrupts shouldn't be
176 * forwarded.
177 */
178 if (irq->enabled && irq_is_pending(irq)) {
179 if (unlikely(irq->target_vcpu &&
180 !irq->target_vcpu->kvm->arch.vgic.enabled))
181 return NULL;
182
183 return irq->target_vcpu;
184 }
185
186 /* If neither active nor pending and enabled, then this IRQ should not
187 * be queued to any VCPU.
188 */
189 return NULL;
190 }
191
192 /*
193 * The order of items in the ap_lists defines how we'll pack things in LRs as
194 * well, the first items in the list being the first things populated in the
195 * LRs.
196 *
197 * A hard rule is that active interrupts can never be pushed out of the LRs
198 * (and therefore take priority) since we cannot reliably trap on deactivation
199 * of IRQs and therefore they have to be present in the LRs.
200 *
201 * Otherwise things should be sorted by the priority field and the GIC
202 * hardware support will take care of preemption of priority groups etc.
203 *
204 * Return negative if "a" sorts before "b", 0 to preserve order, and positive
205 * to sort "b" before "a".
206 */
207 static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)
208 {
209 struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
210 struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
211 bool penda, pendb;
212 int ret;
213
214 spin_lock(&irqa->irq_lock);
215 spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);
216
217 if (irqa->active || irqb->active) {
218 ret = (int)irqb->active - (int)irqa->active;
219 goto out;
220 }
221
222 penda = irqa->enabled && irq_is_pending(irqa);
223 pendb = irqb->enabled && irq_is_pending(irqb);
224
225 if (!penda || !pendb) {
226 ret = (int)pendb - (int)penda;
227 goto out;
228 }
229
230 /* Both pending and enabled, sort by priority */
231 ret = irqa->priority - irqb->priority;
232 out:
233 spin_unlock(&irqb->irq_lock);
234 spin_unlock(&irqa->irq_lock);
235 return ret;
236 }
237
238 /* Must be called with the ap_list_lock held */
239 static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)
240 {
241 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
242
243 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
244
245 list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
246 }
247
248 /*
249 * Only valid injection if changing level for level-triggered IRQs or for a
250 * rising edge, and in-kernel connected IRQ lines can only be controlled by
251 * their owner.
252 */
253 static bool vgic_validate_injection(struct vgic_irq *irq, bool level, void *owner)
254 {
255 if (irq->owner != owner)
256 return false;
257
258 switch (irq->config) {
259 case VGIC_CONFIG_LEVEL:
260 return irq->line_level != level;
261 case VGIC_CONFIG_EDGE:
262 return level;
263 }
264
265 return false;
266 }
267
268 /*
269 * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
270 * Do the queuing if necessary, taking the right locks in the right order.
271 * Returns true when the IRQ was queued, false otherwise.
272 *
273 * Needs to be entered with the IRQ lock already held, but will return
274 * with all locks dropped.
275 */
276 bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
277 unsigned long flags)
278 {
279 struct kvm_vcpu *vcpu;
280
281 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
282
283 retry:
284 vcpu = vgic_target_oracle(irq);
285 if (irq->vcpu || !vcpu) {
286 /*
287 * If this IRQ is already on a VCPU's ap_list, then it
288 * cannot be moved or modified and there is no more work for
289 * us to do.
290 *
291 * Otherwise, if the irq is not pending and enabled, it does
292 * not need to be inserted into an ap_list and there is also
293 * no more work for us to do.
294 */
295 spin_unlock_irqrestore(&irq->irq_lock, flags);
296
297 /*
298 * We have to kick the VCPU here, because we could be
299 * queueing an edge-triggered interrupt for which we
300 * get no EOI maintenance interrupt. In that case,
301 * while the IRQ is already on the VCPU's AP list, the
302 * VCPU could have EOI'ed the original interrupt and
303 * won't see this one until it exits for some other
304 * reason.
305 */
306 if (vcpu) {
307 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
308 kvm_vcpu_kick(vcpu);
309 }
310 return false;
311 }
312
313 /*
314 * We must unlock the irq lock to take the ap_list_lock where
315 * we are going to insert this new pending interrupt.
316 */
317 spin_unlock_irqrestore(&irq->irq_lock, flags);
318
319 /* someone can do stuff here, which we re-check below */
320
321 spin_lock_irqsave(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
322 spin_lock(&irq->irq_lock);
323
324 /*
325 * Did something change behind our backs?
326 *
327 * There are two cases:
328 * 1) The irq lost its pending state or was disabled behind our
329 * backs and/or it was queued to another VCPU's ap_list.
330 * 2) Someone changed the affinity on this irq behind our
331 * backs and we are now holding the wrong ap_list_lock.
332 *
333 * In both cases, drop the locks and retry.
334 */
335
336 if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) {
337 spin_unlock(&irq->irq_lock);
338 spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
339
340 spin_lock_irqsave(&irq->irq_lock, flags);
341 goto retry;
342 }
343
344 /*
345 * Grab a reference to the irq to reflect the fact that it is
346 * now in the ap_list.
347 */
348 vgic_get_irq_kref(irq);
349 list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
350 irq->vcpu = vcpu;
351
352 spin_unlock(&irq->irq_lock);
353 spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
354
355 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
356 kvm_vcpu_kick(vcpu);
357
358 return true;
359 }
360
361 /**
362 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
363 * @kvm: The VM structure pointer
364 * @cpuid: The CPU for PPIs
365 * @intid: The INTID to inject a new state to.
366 * @level: Edge-triggered: true: to trigger the interrupt
367 * false: to ignore the call
368 * Level-sensitive true: raise the input signal
369 * false: lower the input signal
370 * @owner: The opaque pointer to the owner of the IRQ being raised to verify
371 * that the caller is allowed to inject this IRQ. Userspace
372 * injections will have owner == NULL.
373 *
374 * The VGIC is not concerned with devices being active-LOW or active-HIGH for
375 * level-sensitive interrupts. You can think of the level parameter as 1
376 * being HIGH and 0 being LOW and all devices being active-HIGH.
377 */
378 int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
379 bool level, void *owner)
380 {
381 struct kvm_vcpu *vcpu;
382 struct vgic_irq *irq;
383 unsigned long flags;
384 int ret;
385
386 trace_vgic_update_irq_pending(cpuid, intid, level);
387
388 ret = vgic_lazy_init(kvm);
389 if (ret)
390 return ret;
391
392 vcpu = kvm_get_vcpu(kvm, cpuid);
393 if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
394 return -EINVAL;
395
396 irq = vgic_get_irq(kvm, vcpu, intid);
397 if (!irq)
398 return -EINVAL;
399
400 spin_lock_irqsave(&irq->irq_lock, flags);
401
402 if (!vgic_validate_injection(irq, level, owner)) {
403 /* Nothing to see here, move along... */
404 spin_unlock_irqrestore(&irq->irq_lock, flags);
405 vgic_put_irq(kvm, irq);
406 return 0;
407 }
408
409 if (irq->config == VGIC_CONFIG_LEVEL)
410 irq->line_level = level;
411 else
412 irq->pending_latch = true;
413
414 vgic_queue_irq_unlock(kvm, irq, flags);
415 vgic_put_irq(kvm, irq);
416
417 return 0;
418 }
419
420 /* @irq->irq_lock must be held */
421 static int kvm_vgic_map_irq(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
422 unsigned int host_irq)
423 {
424 struct irq_desc *desc;
425 struct irq_data *data;
426
427 /*
428 * Find the physical IRQ number corresponding to @host_irq
429 */
430 desc = irq_to_desc(host_irq);
431 if (!desc) {
432 kvm_err("%s: no interrupt descriptor\n", __func__);
433 return -EINVAL;
434 }
435 data = irq_desc_get_irq_data(desc);
436 while (data->parent_data)
437 data = data->parent_data;
438
439 irq->hw = true;
440 irq->host_irq = host_irq;
441 irq->hwintid = data->hwirq;
442 return 0;
443 }
444
445 /* @irq->irq_lock must be held */
446 static inline void kvm_vgic_unmap_irq(struct vgic_irq *irq)
447 {
448 irq->hw = false;
449 irq->hwintid = 0;
450 }
451
452 int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq,
453 u32 vintid)
454 {
455 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
456 unsigned long flags;
457 int ret;
458
459 BUG_ON(!irq);
460
461 spin_lock_irqsave(&irq->irq_lock, flags);
462 ret = kvm_vgic_map_irq(vcpu, irq, host_irq);
463 spin_unlock_irqrestore(&irq->irq_lock, flags);
464 vgic_put_irq(vcpu->kvm, irq);
465
466 return ret;
467 }
468
469 /**
470 * kvm_vgic_reset_mapped_irq - Reset a mapped IRQ
471 * @vcpu: The VCPU pointer
472 * @vintid: The INTID of the interrupt
473 *
474 * Reset the active and pending states of a mapped interrupt. Kernel
475 * subsystems injecting mapped interrupts should reset their interrupt lines
476 * when we are doing a reset of the VM.
477 */
478 void kvm_vgic_reset_mapped_irq(struct kvm_vcpu *vcpu, u32 vintid)
479 {
480 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
481 unsigned long flags;
482
483 if (!irq->hw)
484 goto out;
485
486 spin_lock_irqsave(&irq->irq_lock, flags);
487 irq->active = false;
488 irq->pending_latch = false;
489 irq->line_level = false;
490 spin_unlock_irqrestore(&irq->irq_lock, flags);
491 out:
492 vgic_put_irq(vcpu->kvm, irq);
493 }
494
495 int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int vintid)
496 {
497 struct vgic_irq *irq;
498 unsigned long flags;
499
500 if (!vgic_initialized(vcpu->kvm))
501 return -EAGAIN;
502
503 irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
504 BUG_ON(!irq);
505
506 spin_lock_irqsave(&irq->irq_lock, flags);
507 kvm_vgic_unmap_irq(irq);
508 spin_unlock_irqrestore(&irq->irq_lock, flags);
509 vgic_put_irq(vcpu->kvm, irq);
510
511 return 0;
512 }
513
514 /**
515 * kvm_vgic_set_owner - Set the owner of an interrupt for a VM
516 *
517 * @vcpu: Pointer to the VCPU (used for PPIs)
518 * @intid: The virtual INTID identifying the interrupt (PPI or SPI)
519 * @owner: Opaque pointer to the owner
520 *
521 * Returns 0 if intid is not already used by another in-kernel device and the
522 * owner is set, otherwise returns an error code.
523 */
524 int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)
525 {
526 struct vgic_irq *irq;
527 unsigned long flags;
528 int ret = 0;
529
530 if (!vgic_initialized(vcpu->kvm))
531 return -EAGAIN;
532
533 /* SGIs and LPIs cannot be wired up to any device */
534 if (!irq_is_ppi(intid) && !vgic_valid_spi(vcpu->kvm, intid))
535 return -EINVAL;
536
537 irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
538 spin_lock_irqsave(&irq->irq_lock, flags);
539 if (irq->owner && irq->owner != owner)
540 ret = -EEXIST;
541 else
542 irq->owner = owner;
543 spin_unlock_irqrestore(&irq->irq_lock, flags);
544
545 return ret;
546 }
547
548 /**
549 * vgic_prune_ap_list - Remove non-relevant interrupts from the list
550 *
551 * @vcpu: The VCPU pointer
552 *
553 * Go over the list of "interesting" interrupts, and prune those that we
554 * won't have to consider in the near future.
555 */
556 static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
557 {
558 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
559 struct vgic_irq *irq, *tmp;
560 unsigned long flags;
561
562 retry:
563 spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
564
565 list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
566 struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
567
568 spin_lock(&irq->irq_lock);
569
570 BUG_ON(vcpu != irq->vcpu);
571
572 target_vcpu = vgic_target_oracle(irq);
573
574 if (!target_vcpu) {
575 /*
576 * We don't need to process this interrupt any
577 * further, move it off the list.
578 */
579 list_del(&irq->ap_list);
580 irq->vcpu = NULL;
581 spin_unlock(&irq->irq_lock);
582
583 /*
584 * This vgic_put_irq call matches the
585 * vgic_get_irq_kref in vgic_queue_irq_unlock,
586 * where we added the LPI to the ap_list. As
587 * we remove the irq from the list, we drop
588 * also drop the refcount.
589 */
590 vgic_put_irq(vcpu->kvm, irq);
591 continue;
592 }
593
594 if (target_vcpu == vcpu) {
595 /* We're on the right CPU */
596 spin_unlock(&irq->irq_lock);
597 continue;
598 }
599
600 /* This interrupt looks like it has to be migrated. */
601
602 spin_unlock(&irq->irq_lock);
603 spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
604
605 /*
606 * Ensure locking order by always locking the smallest
607 * ID first.
608 */
609 if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
610 vcpuA = vcpu;
611 vcpuB = target_vcpu;
612 } else {
613 vcpuA = target_vcpu;
614 vcpuB = vcpu;
615 }
616
617 spin_lock_irqsave(&vcpuA->arch.vgic_cpu.ap_list_lock, flags);
618 spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
619 SINGLE_DEPTH_NESTING);
620 spin_lock(&irq->irq_lock);
621
622 /*
623 * If the affinity has been preserved, move the
624 * interrupt around. Otherwise, it means things have
625 * changed while the interrupt was unlocked, and we
626 * need to replay this.
627 *
628 * In all cases, we cannot trust the list not to have
629 * changed, so we restart from the beginning.
630 */
631 if (target_vcpu == vgic_target_oracle(irq)) {
632 struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
633
634 list_del(&irq->ap_list);
635 irq->vcpu = target_vcpu;
636 list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
637 }
638
639 spin_unlock(&irq->irq_lock);
640 spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
641 spin_unlock_irqrestore(&vcpuA->arch.vgic_cpu.ap_list_lock, flags);
642 goto retry;
643 }
644
645 spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
646 }
647
648 static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
649 {
650 if (kvm_vgic_global_state.type == VGIC_V2)
651 vgic_v2_fold_lr_state(vcpu);
652 else
653 vgic_v3_fold_lr_state(vcpu);
654 }
655
656 /* Requires the irq_lock to be held. */
657 static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
658 struct vgic_irq *irq, int lr)
659 {
660 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
661
662 if (kvm_vgic_global_state.type == VGIC_V2)
663 vgic_v2_populate_lr(vcpu, irq, lr);
664 else
665 vgic_v3_populate_lr(vcpu, irq, lr);
666 }
667
668 static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
669 {
670 if (kvm_vgic_global_state.type == VGIC_V2)
671 vgic_v2_clear_lr(vcpu, lr);
672 else
673 vgic_v3_clear_lr(vcpu, lr);
674 }
675
676 static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
677 {
678 if (kvm_vgic_global_state.type == VGIC_V2)
679 vgic_v2_set_underflow(vcpu);
680 else
681 vgic_v3_set_underflow(vcpu);
682 }
683
684 static inline void vgic_set_npie(struct kvm_vcpu *vcpu)
685 {
686 if (kvm_vgic_global_state.type == VGIC_V2)
687 vgic_v2_set_npie(vcpu);
688 else
689 vgic_v3_set_npie(vcpu);
690 }
691
692 /* Requires the ap_list_lock to be held. */
693 static int compute_ap_list_depth(struct kvm_vcpu *vcpu,
694 bool *multi_sgi)
695 {
696 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
697 struct vgic_irq *irq;
698 int count = 0;
699
700 *multi_sgi = false;
701
702 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
703
704 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
705 spin_lock(&irq->irq_lock);
706 /* GICv2 SGIs can count for more than one... */
707 if (vgic_irq_is_sgi(irq->intid) && irq->source) {
708 int w = hweight8(irq->source);
709
710 count += w;
711 *multi_sgi |= (w > 1);
712 } else {
713 count++;
714 }
715 spin_unlock(&irq->irq_lock);
716 }
717 return count;
718 }
719
720 /* Requires the VCPU's ap_list_lock to be held. */
721 static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
722 {
723 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
724 struct vgic_irq *irq;
725 int count;
726 bool npie = false;
727 bool multi_sgi;
728 u8 prio = 0xff;
729
730 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
731
732 count = compute_ap_list_depth(vcpu, &multi_sgi);
733 if (count > kvm_vgic_global_state.nr_lr || multi_sgi)
734 vgic_sort_ap_list(vcpu);
735
736 count = 0;
737
738 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
739 spin_lock(&irq->irq_lock);
740
741 /*
742 * If we have multi-SGIs in the pipeline, we need to
743 * guarantee that they are all seen before any IRQ of
744 * lower priority. In that case, we need to filter out
745 * these interrupts by exiting early. This is easy as
746 * the AP list has been sorted already.
747 */
748 if (multi_sgi && irq->priority > prio) {
749 spin_unlock(&irq->irq_lock);
750 break;
751 }
752
753 if (likely(vgic_target_oracle(irq) == vcpu)) {
754 vgic_populate_lr(vcpu, irq, count++);
755
756 if (irq->source) {
757 npie = true;
758 prio = irq->priority;
759 }
760 }
761
762 spin_unlock(&irq->irq_lock);
763
764 if (count == kvm_vgic_global_state.nr_lr) {
765 if (!list_is_last(&irq->ap_list,
766 &vgic_cpu->ap_list_head))
767 vgic_set_underflow(vcpu);
768 break;
769 }
770 }
771
772 if (npie)
773 vgic_set_npie(vcpu);
774
775 vcpu->arch.vgic_cpu.used_lrs = count;
776
777 /* Nuke remaining LRs */
778 for ( ; count < kvm_vgic_global_state.nr_lr; count++)
779 vgic_clear_lr(vcpu, count);
780 }
781
782 /* Sync back the hardware VGIC state into our emulation after a guest's run. */
783 void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
784 {
785 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
786
787 WARN_ON(vgic_v4_sync_hwstate(vcpu));
788
789 /* An empty ap_list_head implies used_lrs == 0 */
790 if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
791 return;
792
793 if (vgic_cpu->used_lrs)
794 vgic_fold_lr_state(vcpu);
795 vgic_prune_ap_list(vcpu);
796 }
797
798 /* Flush our emulation state into the GIC hardware before entering the guest. */
799 void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
800 {
801 WARN_ON(vgic_v4_flush_hwstate(vcpu));
802
803 /*
804 * If there are no virtual interrupts active or pending for this
805 * VCPU, then there is no work to do and we can bail out without
806 * taking any lock. There is a potential race with someone injecting
807 * interrupts to the VCPU, but it is a benign race as the VCPU will
808 * either observe the new interrupt before or after doing this check,
809 * and introducing additional synchronization mechanism doesn't change
810 * this.
811 */
812 if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
813 return;
814
815 DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
816
817 spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
818 vgic_flush_lr_state(vcpu);
819 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
820 }
821
822 void kvm_vgic_load(struct kvm_vcpu *vcpu)
823 {
824 if (unlikely(!vgic_initialized(vcpu->kvm)))
825 return;
826
827 if (kvm_vgic_global_state.type == VGIC_V2)
828 vgic_v2_load(vcpu);
829 else
830 vgic_v3_load(vcpu);
831 }
832
833 void kvm_vgic_put(struct kvm_vcpu *vcpu)
834 {
835 if (unlikely(!vgic_initialized(vcpu->kvm)))
836 return;
837
838 if (kvm_vgic_global_state.type == VGIC_V2)
839 vgic_v2_put(vcpu);
840 else
841 vgic_v3_put(vcpu);
842 }
843
844 int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
845 {
846 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
847 struct vgic_irq *irq;
848 bool pending = false;
849 unsigned long flags;
850
851 if (!vcpu->kvm->arch.vgic.enabled)
852 return false;
853
854 if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last)
855 return true;
856
857 spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
858
859 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
860 spin_lock(&irq->irq_lock);
861 pending = irq_is_pending(irq) && irq->enabled;
862 spin_unlock(&irq->irq_lock);
863
864 if (pending)
865 break;
866 }
867
868 spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
869
870 return pending;
871 }
872
873 void vgic_kick_vcpus(struct kvm *kvm)
874 {
875 struct kvm_vcpu *vcpu;
876 int c;
877
878 /*
879 * We've injected an interrupt, time to find out who deserves
880 * a good kick...
881 */
882 kvm_for_each_vcpu(c, vcpu, kvm) {
883 if (kvm_vgic_vcpu_pending_irq(vcpu)) {
884 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
885 kvm_vcpu_kick(vcpu);
886 }
887 }
888 }
889
890 bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int vintid)
891 {
892 struct vgic_irq *irq;
893 bool map_is_active;
894 unsigned long flags;
895
896 if (!vgic_initialized(vcpu->kvm))
897 return false;
898
899 irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
900 spin_lock_irqsave(&irq->irq_lock, flags);
901 map_is_active = irq->hw && irq->active;
902 spin_unlock_irqrestore(&irq->irq_lock, flags);
903 vgic_put_irq(vcpu->kvm, irq);
904
905 return map_is_active;
906 }
907